views:

78

answers:

4

So I have a class where I instantiate a variable callback like so:

public var callback:Function;

So far so good. Now, I want to add an event listener to this class and test for existence of the callback. I'm doing like so:

this.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent) : void {
        if (callback) {
            // do some things
        }
});

This works great, doesn't throw any errors, but everywhere I test for callback I get the following warning:

3553: Function value used where type Boolean was expected.  
Possibly the parentheses () are missing after this function reference.

That bugged me, so I tried to get rid of the warning by testing for null and undefined. Those caused errors. I can't instantiate a Function as null, either.

I know, I know, real programmers only care about errors, not warnings. I will survive if this situation is not resolved. But it bothers me! :) Am I just being neurotic, or is there actually some way to test whether a real Function has been created without the IDE bitching about it?

A: 

Have you tried:

if (typeof callback == "function") {
  // do some things
}

?

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/operators.html#typeof

Hooray Im Helping
@Hooray: Thanks, but I need to know more than typeof. As it stands right now, even when `callback` is not empty, `(typeof callback == "function")` will always return true. What I need to know is whether there is a real, callable function named callback.
Robusto
@Hooray: Actually, I should rephrase. What you have actually works as a conditional, but the IDE still complains with the same error. So what you have is no different in that respect from `if (callback)`. Thanks for the effort, though.
Robusto
+2  A: 

Similar to using typeof:

if(callback is Function){

}

I believe should evaluate to true if the function exists and is a function and false if it is null or is not a function. (although if that doesn't work try if(callback && callback is function){}

quoo
Thanks, this works. It needs the Function datatype to be capitalized, however. I'll edit your answer accordingly.
Robusto
thanks! i can't seem to type anything correctly today.
quoo
+2  A: 
if( !(callback == null)){
 // do something
}
www.Flextras.com
@Flextras: This works and the IDE doesn't complain. I gave quoo the check because his solution is simpler. But you get +1 and my thanks.
Robusto
yep, this totally works too, and i'm a she, not a he.
quoo
@quoo: My apologies for the misapplied gender. Gave you an extra bump for the *faux pas*.
Robusto
I think I just got extra checks because you confused Quoo; not me.;) Her answer is slightly more elegant for sure. I just wish I thought of that first.
www.Flextras.com
+1  A: 

There's already an answer that works, but I thought I'd mention that you can also stop the warning from occurring by explicitly casting the result to a Boolean.

if (Boolean(callback)) {
// do something
}
Nyeski
All good ideas. Thanks.
Robusto