Question

In languages like Java, C++ and etc there is the ability to provide, for example, a toInt() function to allow your code to be converted neatly by language features into a given primitive type. (In this example, an Int.)

That is, if you had myObject() with the standard casting function toInt() declared, then calls like Int(myObject) would just work. This is much more relevant to situations where you just want to forget about the cast altogether and just get something done - someVar:Int = myObject + 3 ... for an arbitrary example.

I've searched through AS3 docs and done some searching outside that but it appears there is no such sets of functions, interfaces, or other such things easily accessible in AS3. Does anyone know such a thing? It seems like essential knowledge in any language that supports such casting features and I'm at my wit's end with the verbosity of writing a partially qualified name like myObject.toInt() in the midst of mathematical work.

Was it helpful?

Solution

It's a common misconception that operator overloading in AS3 is impossible. It's not, but it's not entirely common practice, and it doesn't work as in other languages.

AS3 is "gradually typed". This means that you can specify type when you want to, you don't have to, and when performing operations on two different types it'll infer/cast for you in a logical way.

For objects, AS3 provides the valueOf():Object and toString():String functions which allow you to define the automatic handling of casting. The former provides the "primitive value of the object" while the latter defines the "String representation of the Object".

The default value for both is the String "[object ClassName]", but you can override this default. Here's an example:

package
{
  import flash.display.Sprite;
  import flash.utils.getQualifiedClassName;

  public class Main extends Sprite
  {
    public function Main():void
    {
      trace("-----------------------------");
      var foo = new MyClass();
      trace("foo is: ");
      trace(foo);
      trace("foo+foo is:");
      trace(foo+foo);
      trace("foo+foo+'--' is:");
      trace(foo+foo+"--");
      trace("'--'+foo+foo is:");
      trace("--"+foo+foo);
      trace("Math.PI/foo:");
      trace(Math.PI/foo);
      trace("'5'+foo is:");
      trace('5'+foo);
      trace("false || foo is:");
      trace((false || foo));
      trace("foo | 0xC is:");
      trace(foo | 0xC);
      trace("typeof(foo) is:");
      trace(typeof(foo));
      trace("getQualifiedClassName(foo) is:");
      trace(getQualifiedClassName(foo));
    }

  }
}

class MyClass {
  public function valueOf():Object { return 3; }
  public function toString():String { return "three"; }
}

And the trace output is:

-----------------------------
foo is: 
three
foo+foo is:
6
foo+foo+'--' is:
6--
'--'+foo+foo is:
--threethree
Math.PI/foo:
1.0471975511965976
'5'+foo is:
5three
false || foo is:
three
foo | 0xC is:
15
typeof(foo) is:
object
getQualifiedClassName(foo) is:
Main.as$30::MyClass

The Boolean interpretation is interesting, but any non-null Object (or String) is true, so actually it works out. Whether the runtime calls valueOf() or toString() appears to be dependent on the types of the other arguments to the operators.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top