2.5.1 Optional Arguments

Optional arguments are declared by prefixing an argument identifier with a question mark ?:

class Main {
  static public function main() {
    // (?i : Int, ?s : String) -> String
    trace(test()); // i: null, s: null
    trace(test(1)); // i: 1, s: null
    trace(test(1, "foo")); // i: 1, s: foo
    trace(test("foo")); // i: null, s: foo

  static function test(?i:Int, ?s:String) {
    return "i: " + i + ", s: " + s;

Function test has two optional arguments: i of type Int and s of String. This is directly reflected in the function type output by line 4. This example program calls test four times and prints its return value.

  1. The first call is made without any arguments.
  2. The second call is made with a singular argument 1.
  3. The third call is made with two arguments 1 and "foo".
  4. The fourth call is made with a singular argument "foo".

The output shows that optional arguments which are omitted from the call have a value of null. This implies that the type of these arguments must admit null as value, which raises the question of its nullability. The Haxe Compiler ensures that optional basic type arguments are nullable by inferring their type as Null<T> when compiling to a static target.

While the first three calls are intuitive, the fourth one might come as a surprise; optional arguments can be skipped if the supplied value is assignable to a later argument.