[Laszlo-dev] Undefined pattern in AS3

P T Withington ptw at pobox.com
Mon Aug 25 07:46:46 PDT 2008

I thought I ran into this too, but the error turned out to be  
something different:


Are you saying that in AS3 if you replace `void 0` with `undefined`  
you won't get the coercion error?  That would be totally strange,  
because `===` is not supposed to do any coercion.  It should just  
return `false` if it is asked to compare unrelated types.

I think this is really just a bad choice of error message on the part  
of the compiler.  What it is trying to tell you is: "You declared  
`old` to be of type `Object`, and on the next line, you are asking if  
it could possibly be of type `Void`.  That is impossible."

[So, in AS3, for any class `X`, if you declare something as of type  
`X` you are implicitly declaring it as `X?` (to use JS2 syntax).  That  
is, any var that is declared as a subclass of `Object` is also allowed  
to be of type `Null`... but not of type `Void`.

Brendan takes full responsibility for the 'Javascript has two nulls  
mess'.  But it appears that Flex has made it a bit messier, by making  
`Null` a pseudo-subclass of `Object`, but `Void` not?]

I think you are correct in using `*` as the type declaration when you  
want to admit `void 0` as a possible value (although that means you  
open it up to other "primitive" types also, which may or may not be  
what you want).

On 2008-08-25, at 10:04EDT, André Bargull wrote:

> In my recent changeset for RegExp-support in swf8, you'll get an  
> error, if you compile the lfc for swf9:
> The simple cache class I've added accepts "Objects" as values, so I  
> coded:
>> function put (key:String, value:Object) :Object {...}
>> function get (key:String) :Object {...}
> And in these functions, I've used the "undefined"-pattern described  
> in [1]:
>> var old:Object = this.get(key);
>> if (*old === void(0)*) {
> But this gave me an AS3 compiler-error [2]:
>> 1118    Implicit coercion of a value with static type Object to a  
>> possibly unrelated type void.
> To get around the compiler error, I've just changed the type- 
> declaration to "*" (maybe even more reasonable). So far so good, but  
> we've used the "void 0"-pattern all along in our code. Therefore do  
> you think it's possible to update the compiler to change "void 0" to  
> "undefined" for JS2-targets, resp. the other way around, so  
> "undefined" to "void 0" for JS1-targets (possibly more  
> straightforward)? Just to ensure the "one code, multiple runtimes"  
> idea in OpenLaszlo.
> - André
> [1] http://pt.withy.org/ptalk/archives/2005/06/dont_assume_undefined_is_undefined.html
> [2] http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/compilerErrors.html

More information about the Laszlo-dev mailing list