views:

231

answers:

1

Hello, I am currently doing something like this:

myFunc(tracer);

function tracer(message:String):void{
 trace(message);
}

function myFunc(callback:Function):void{
 callback("Hello");
}

Now, this works fine. But how can the function myFunc know, if the given callback function accepts the correct number and type of arguments in its signature?

I want to avoid that I call something like this somewhere in my code:

myFunc(tracer2);

function tracer2():void{
 trace("done");
}

function myFunc(callback:Function):void{
 // Argument mismatch!
 callback("Hello");
}

Is there a way to do something like this, in order to use compiler warnings/error messages and thus avoid exceptions at runtime?

// Won't work :-(
function myFunc(callback(message:String):Function):{
 callback("Hello");
}
+2  A: 

My understanding is that this is a situation that can only be caught at runtime. AS3 offers no mechanism for this kind of type-checking. I would suggest that if this is important, that you get compile-time checking by passing an object with a strongly typed interface rather than simply passing a function, where the type information you need will be lost.

spender
Too bad :-(Wished, AS3 was a bit more powerful.