0

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.

That Guy
  • 103
  • 1
  • 8
  • There is *some* casting: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f87.html – Anil Dec 23 '13 at 21:02
  • It's just that if you have a custom class, AS can't explicitly know how to cast it to any other primitive type. – Anil Dec 23 '13 at 21:09
  • That article was more useless than nothing. What it says is that things in the same inheritance tree have some ability to be cast to each other (probably coerced, too, if I had to guess) and that the primitives can be cast into one another. While that is of some relevance, it'd be nice - for example - to make a complex color representative class that can be coerced neatly into a number for computational media. (Just for an off-the-cuff example.) – That Guy Dec 23 '13 at 21:11

1 Answers1

2

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.

Jeff Ward
  • 16,563
  • 6
  • 48
  • 57
  • I'm not sure why I didn't trust them to put it at the highest level of the general hierachy, the `Object` class. Just as a supplement, the relevant docs for the default function definitions can be found [here](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Object.html#methodSummary) – That Guy Dec 23 '13 at 21:21
  • Thanks for reminding me, I added links to the docs into the answer. – Jeff Ward Dec 23 '13 at 22:01