views:

62

answers:

2

It seems like there might be a way, but I'm not seeing it. I have, in the past, used the valueOf() and toString() methods on Object to cause custom objects to behave in numbers or strings based on context, but I'd like to do more.

A: 

Basically no. Final is final so they cannot be extended. You could make a class which has all the same methods as the Number class, but it still wouldn't BE a Number as far as the compiler is concerned.

To be honest there should never be a reason that you should need to extend from these classes.

As far as proxies go you could consider making a factory class which returns a pre-formatted string/number eg:

var myString:String= StringFactory.asCurrency("50"); // "$50.00"
Groady
There are plenty of reasons to want a class to behave like a primitive. A simple example would be a desire to have a subclass of Number (like uint) that has a restricted value set, or a subclass of String that must have a given length. You can pseduo-subclass Array via Proxy to put extra behavior into the array-access operator -- I am looking for a way to perform similar tasks.
JMHNilbog
A: 

hi,

as already stated by groady, this is not possible ... also not in the scenarios you described ... but the thing is, that at runtime, the type detection mechanism is pretty easy ... lookup the traits object, and check whether it matches a class/subclass, or whether it explicitely implements and interface ... in any other case, you will have errors ... you can use proxies to implement your own array access ... however, they will not be arrays, thus passing them to a function that expects Array, will cause errors ... also, in AS3 you cannot overload operators, so you will really have a hard time ... you could create a class for numeric values, but then manipulating it would require methods as add, subtract etc. ... there is however a related request on jira ... still, this will not solve your problem entirely, because you cannot control the way an object responds to operators ... if you compile in ECMA compatibility mode, you probable will be able to bypass the strict runtime type checks, but on the other hand, you will lose a lot of speed ... the best thing probably really is creating a class that has to be manipulated through methods instead of operators ... not too comfortable, but the best AS3 offers ...

back2dos