Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't typed optional arguments have a default of Null?

In ActionScript 3, when you declare an optional argument by giving it a default value, the value null cannot be used on typed arguments.

function Action(Param:int=null){
    // 1184: Incompatible default value of type Null where int is expected.
}
function Action(Param:int=0){
    // No compiler errors
} 

Any workarounds for this, or general purpose values that can apply to all data types?

like image 886
Robin Rodricks Avatar asked Jun 16 '09 18:06

Robin Rodricks


People also ask

Is it mandatory to specify a default value to optional parameter?

OptionalAttribute parameters do not require a default value.

Why optional should not be used for parameters?

You should almost never use it as a field of something or a method parameter. So the answer is specific to Optional: it isn't "a general purpose Maybe type"; as such, it is limited, and it may be limited in ways that limit its usefulness as a field type or a parameter type.

Can typescript optional be Null?

With optional you can leave the argument out, or pass undefined , BUT NOT null .

How do you pass an optional argument in Python?

You can define Python function optional arguments by specifying the name of an argument followed by a default value when you declare a function. You can also use the **kwargs method to accept a variable number of arguments in a function.


2 Answers

You can change your int to Number and then can set it to NaN which is a special number that means 'not a number' and this can represent your null state for a Number.

To check if something is NaN, you must use the isNaN() function and not val == NaN, or you will not get what you expect.

function Action(param:Number = NaN) : void {
    trace(param);
}

For all other objects, you can set them to null, but 'primitive' numbers are handled differently in Actionscript.

like image 97
Kekoa Avatar answered Sep 28 '22 02:09

Kekoa


int variables cannot be null, that's why you get that error, only reference types like objects can be null

Instead you can use NaN as a special number instead of null. If you want to check if something is NaN you mus use the isNaN function.

like image 37
albertein Avatar answered Sep 28 '22 01:09

albertein