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?
OptionalAttribute parameters do not require a default value.
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.
With optional you can leave the argument out, or pass undefined , BUT NOT null .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With