is supported in part by

August 01, 2008

ActionScript 3.0 String-concatenation error gotcha

Take a look at this code:

trace(1 + + 2);  // Output: 3

It may seem strange to us, but ActionScript considers it legal. It assumes you mean:

trace(1 + (+2));

which produces the value 3.

Now take a look at this code:

// Oops! One too many + signs...
trace("Hello" +
      + " world");

Once again, ActionScript assumes you mean:

trace("Hello" +
      (+ " world"));

which yields the following somewhat misleading error:

1067: Implicit coercion of a value of type String to an unrelated type Number.	

The error occurs because ActionScript considers '(+ "world")' a datatype-conversion operation, not a String concatenation. The fix is simple: remove one + sign.

      + " world");

But why does ActionScript consider '(+ "world")' a datatype conversion? Francis Cheng explains here...

Posted by moock at August 1, 2008 04:39 AM