Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting default value for function argument in actionscript 3

Is there any way that I can reference a var or const as the default value for a function argument in actionscript 3.

I can define default values like null, string, int.

function a( b = null ) {
   blah...
}

But what I want to do is

function a( b = function(){} ) {
    blah...
}

which it seems like there would be a way to do. Presumably through a const

like image 793
AlexH Avatar asked Dec 12 '08 18:12

AlexH


2 Answers

Oddly enough it seems you can't do that, atleast I couldn't get it to work, it won't accept any references to static functions as a default value.

The best I could do was this:

public function myFunction(functionArgument:Function = null):void {
    if (functionArgument != null) {
        functionArgument();
    } else {
        defaultFunction();
    }
}

As a sidenote I just discovered that you in fact can declare functions like this:

public static const STATICFUNC:Function = function():void { trace("i'm static!") };

But that seems to work the same way as declaring them the sane way, so no luck there either.

like image 147
grapefrukt Avatar answered Nov 15 '22 10:11

grapefrukt


Parameter defaults must be compile-time constants, because the compiler has to put in the default value when the code is compiled. This means static functions, no matter how predictable they are, cannot be used as parameter defaults. Compile-time constants are values the compiler knows about by inspecting your code, but not actually running it.

like image 42
bzlm Avatar answered Nov 15 '22 09:11

bzlm