Search code examples
nullablehaxe

Optional arguments without ? identifier


In the Haxe 3 manual at http://haxe.org/manual/types-function-default-values.html, we have static function test(?i = 12, s = "bar").

Why isn't it static function test(?i = 12, ?s = "bar") or static function test(i = 12, s = "bar")?


Solution

  • Actually, there's a subtle difference between ?i=1 and i=1:

    • function test(?i=1):String is of type Null<Int> -> String
    • function test(i=1):String is of type Int -> String

    While ?i indicates an optional argument – and that always implies nullability – i=1 indicates a default value and should eliminate the need for a Null<T> box.

    See Optional Arguments and Nullability and an example.