This is a response to Maploop’s weblog, where he has an entry talking about addition and the $ symbol. The comments there seem to be broken, so I’m re-posting on my blog here.
Why not just override valueOf, and then you can use them as custom addition datatypes? Haven’t tried myself, so don’t know if it works or not…
:: tries ::
Hrm, works in AS1:
function Cow() { } Cow.prototype.valueOf = function() { return 2; }; var a = new Cow(); var b = new Cow(); trace(a + b); // 4
Now AS3…
:: tries ::
Yep, works in AS3 as well:
package { public class Cow { public function Cow() { } public function valueOf():Number { return 2; } } } var a:Cow = new Cow(); var b:Cow = new Cow(); trace(a + b); // 4
Try to add this as complex data (vector) and get return value as vector too. It didn’t work on complex data types.
I do exactly that in some classes
http://maashaack.googlecode.com/svn/trunk/ES4a/src/system/Version.as
here the Version class is interesting, because as soon as you override valueOf
you can use the class in much more interesting ways even if we don’t have yet
operators overloading (we will have them in AS4)
ex:
v1 = new Version( 1, 0 );
v2 = new Version( 2, 0 );
if( v1