[C#] Optional arguments? #4623

Open
YellowAfterlife opened this Issue Nov 2, 2015 · 1 comment

Projects

None yet

4 participants

@YellowAfterlife
Contributor

Hi. This is a question regarding use of HXCS for generating code for use from C# codebase.

Currently,

@:nativeGen class Main {
    static function test(a:Int, ?b:String):Void {
        cs.system.Console.WriteLine(a + " " + b);
    }
    public static function main() {
        test(1);
        test(2, "2");
    }
}

produces code like so:

public class Main {
    public static void test(int a, string b) {
        global::System.Console.WriteLine(((string) (global::haxe.lang.Runtime.concat(global::haxe.lang.Runtime.concat(global::haxe.lang.Runtime.toString(a), " "), b)) ));
    }
    public static void main() {
        unchecked {
            global::Main.test(1, null);
            global::Main.test(2, "2");
        }
    }   
}

Which is sort of okay, but less so if the resulting API is intended to be bearable to work with and multiple functions spot a multitude of optional parameters for convenience (which, in this case, becomes inconvenience, as they have to be filled out at all times).

It would be nice if HXCS could generate overloaded function copies with optional parameter values inserted, i.e.

public static void test(int a, string b) {
    // ...
}
public static void test(int a) {
    string b = null;
    // ...
}

Or at least generate small overload functions that call the main function with the optional parameters filled out,

public static void test(int a, string b) {
    // ...
}
public static void test(int a) {
    test(a, null);
}

and leave inlining to JIT.

It is possible to accomplish both approaches on per-project basis by creating those extra functions by yourself (either manually or via macro) and renaming them to become overloads via the @:native metadata, but, as you can guess, this can get a bit quirky.

Have a nice day.

@waneck waneck was assigned by nadako Nov 2, 2015
@YellowAfterlife
Contributor

Current workaround macros, should anyone need it:
https://gist.github.com/anonymous/4837c6dd39e56b1ffe28
(untyped __cs__ due to fact that I've no idea how to get function arguments to work while using haxe.macro.Context.parse)

@Simn Simn modified the milestone: 3.4 Feb 23, 2016
@Simn Simn modified the milestone: 3.4, 4.0 Jan 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment