haXe

Optional Arguments

Some function parameters can be made optional by using a question mark ? before the parameter name :

class Test {
    static function foo( x : Int, ?y : Int ) {
        trace(x+","+y);
    }
    static function main() {
        foo(1,2); // trace 1,2
        foo(3); // trace 3,null
    }
}

Although it is advised to put optional parameters at the end of the function parameters, you can use them in the beginning or in the middle also.

Also, optional parameters are independent in haXe. It means that one optional parameter can be used without providing a previous one :

    function foo( ?x : A, ?y : B ) {
    }

    foo(new A()); // same as foo(new A(),null);
    foo(new B()); // same as foo(null, new B());
    foo(); // same as foo(null,null);
    foo(new C()); // compile-time error
    foo(new B(),new A()); // error : the order must be preserved

However, such usages of optional arguments could be considered quite advanced.

«« Properties | Conditional Compilation »»

version #1119, modified 2008-05-03 10:48:02 by ponticelli