[haXe] casting attached MovieClips
Nicolas Cannasse
ncannasse at motion-twin.com
Wed Apr 26 08:35:40 CEST 2006
> I know the previous example doesn't compile (the attachMovie() call is
> ok, but the cast fails), but is there any better way of doing this
> rather than casting everything manually (without sacrifing type
> safety)? I guess I'm looking for a mechanism similar to C++ function
> templates, where one could write (hypothetically)
>
> public static void doAttach<T>(mc:MovieClip):T {
> return cast(mc.attachMovie(T + "", "foo", mc.getNextHighestDepth(), null), T);
> }
>
> public static main() {
> var clip:MyClip = doAttach<MyClip>(mc);
> }
That's not so easy. In particular, the type T can't be used as an
expression in method body right now. However, the following should work:
public static function doAttach<T>(mc:MovieClip,cl : Dynamic) : T {
var depth = mc.getNextHighestDepth();
var sub = mc.attachMovie(cl.__name__.join("."),"foo"+depth,depth);
if( !Std.is(sub,cl) )
throw "doAttach failed";
return untyped sub;
}
Then :
doAttach<MyClip>(root,MyClip)
Nicolas
More information about the Haxe
mailing list