Right. String was not the right example. AS3 makes all strings 'objects', so they are by default nullable, but Number and Boolean are not. I expanded your example a little bit:

<canvas debug="true">
  <attribute name="values" value="[void 0, null, false, 0,'']" />
  <handler name="oninit">
for (var i = 0, l = this.values.length; i &lt; l; i++) { var v = this.values[i]; this.meth(v, v, v, v); }
  </handler>
  <method name="meth" args="v, s:String, n:Number, b:Boolean" >
Debug.info("%#w passed as a: String is %#w; Number is %#w; Boolean is %#w", v, s, n, b);
  </method>
</canvas>

Which in swf8/dhtml yields:

INFO: (void 0) passed as a: String is (void 0); Number is (void 0); Boolean is (void 0)
INFO: null passed as a: String is null; Number is null; Boolean is null
INFO: false passed as a: String is false; Number is false; Boolean is false
INFO: 0 passed as a: String is 0; Number is 0; Boolean is 0
INFO: '' passed as a: String is ''; Number is ''; Boolean is ''

And in swf9 yields:

INFO: (void 0) passed as a: String is null; Number is NaN; Boolean is false
INFO: null passed as a: String is null; Number is 0; Boolean is false
INFO: false passed as a: String is 'false'; Number is 0; Boolean is false
INFO: 0 passed as a: String is '0'; Number is 0; Boolean is false
INFO: '' passed as a: String is ''; Number is 0; Boolean is false

Actually, this is either a compiler bug, or a good doc example... I think the former. We should make the js1 back-end implement coercions for types. Hm: http://jira.openlaszlo.org/jira/browse/LPP-5682

On 2009-01-09, at 13:49EST, André Bargull wrote:

Not really...

This testcase prints:
s was null
and _*not*_
s was 'null'

<canvas debug="true" oninit="this.meth(null)" >
<method name="meth" args="s:String" >
  Debug.write("s was %w", s);
</method>
</canvas>

The only special thing about Strings in AS3, is how `undefined` is handled.
var t:String = undefined; // this will be changed to `null`
var r:String = String(undefined); // this is really `undefined`


Finally, you can declare the types of your arguments. These types are ignored in the JS1 back-ends, but they are enforced in JS2 (swf9).

  function foo (required:String, optional:Boolean=false)

Caution: You will get a type error, but _only_ if there is not a coercion available. Hence, passing `null` as the first argument to foo above will cause it to be called with the string "null", not the null value. If you want a nullable type, you should leave the type undeclared (for now...).



Reply via email to