Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C#] Optional arguments? #4623

Closed
YellowAfterlife opened this issue Nov 2, 2015 · 1 comment

Comments

@YellowAfterlife
Copy link
Contributor

commented Nov 2, 2015

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.

@YellowAfterlife

This comment has been minimized.

Copy link
Contributor Author

commented Dec 10, 2015

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 milestones: 3.4, 4.0 Jan 9, 2017
@Simn Simn modified the milestones: Release 4.0, Bugs Apr 17, 2018
RealyUniqueName added a commit that referenced this issue Jun 28, 2019
…ix auto-overloading constructors (#4623)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.