A while ago I posted about casting failing if you cast the result of attachMovie() but only in certain cases.

I had the same thing happen today, so I opened it up in FLASM, and I think I can see what is happening:

push 'sp'
getVariable
trace
push 'sb', 0.0, 'getNextHighestDepth'
callFunction
push 'sb', 'com'
getVariable
push 'domain'
getMember
push 'controls'
getMember
push 'ScrollBar'
getMember
push 'LINKAGE'
getMember
push 3, 'attachMovie'
callFunction
push 1, 'com'
getVariable
push 'domain'
getMember
push 'controls'
getMember
push 'ScrollBar'
callMethod
varEquals

As you can see here, it seems instead of performing a cast operation (and “cast” is a keyword you do see in the FLASM output), it actually tries to perform a conversion (as you see with Array or Boolean), treating com.domain.controls.ScrollPane as a function, and applying it to the result from the attachMovie() operation. This results in the undefined/null value we are experiencing because in itself, this function has no return value unless used with the new keyword as a constructor.

As I stated in the previous post on this subject, splitting the code onto two lines seems to fix the problem (i.e. attachMovie() then cast on the next line). Is this a compiler bug? Has anyone more experience with the inner workings of the Flash Player? Why do Macromedia’s v2 components not suffer this, I have a feeling it is to do with the inline dynamic dereferencing, maybe #initclip needs to be brought in for AS2?