intellij-haxe icon indicating copy to clipboard operation
intellij-haxe copied to clipboard

extern overloads

Open mikaib opened this issue 5 months ago • 11 comments

Heya,

There are some externs that I'm using that make use of the "overload" functionality provided in Haxe. Currently this is not supported.

class Test {
    public extern inline overload function test(a: String): Void {
        trace("a string");
    }

    public extern inline overload function test(a: Int): Void {
        trace("an int");
    }

    public extern inline overload function test(a: Int, b: String): Void {
        trace("both!");
    }

    public function new() {
        test("Hello");
        test(42);
        test(42, "Hello");
    }
}

In this example I'm using inline to demo it with purely Haxe code (makes testing easier).

mikaib avatar Jun 14 '25 13:06 mikaib

Luckily, I started implementing this today and was just creating an issue when I saw this :)

barisyild avatar Jun 14 '25 19:06 barisyild

not sure i understand the issue, what is the problem that needs fixing ?

m0rkeulv avatar Jun 14 '25 19:06 m0rkeulv

not sure i understand the issue, what is the problem that needs fixing ?

This feature is similar to Java's function feature, we can create functions with the same name and different parameters.

The IDE only accepts the first one and gives a type issue highlight.

barisyild avatar Jun 14 '25 19:06 barisyild

Heya,

There are some externs that I'm using that make use of the "overload" functionality provided in Haxe. Currently this is not supported.

class Test { public extern inline overload function test(a: String): Void { trace("a string"); }

public extern inline overload function test(a: Int): Void {
    trace("an int");
}

public extern inline overload function test(a: Int, b: String): Void {
    trace("both!");
}

public function new() {
    test("Hello");
    test(42);
    test(42, "Hello");
}

} In this example I'm using inline to demo it with purely Haxe code (makes testing easier).

Oh, i can't reproduce issue with your sample.

Let me prepare a new example.

barisyild avatar Jun 14 '25 19:06 barisyild

this is how it looks to me when trying to test with return types Image

m0rkeulv avatar Jun 14 '25 19:06 m0rkeulv

Okay here is new reproduce code

class Test {
    public function new() {
        Test2.test("Hello");
        Test2.test(42);
        Test2.test(42, "Hello");
    }
}

class Test2 {
    public overload extern inline static function test(a: String): Void {
        trace("a string");
    }

    public overload extern inline static function test(a: Int): Void {
        trace("an int");
    }

    public overload extern inline static function test(a: Int, b: String): Void {
        trace("both!");
    }
}

Image

barisyild avatar Jun 14 '25 19:06 barisyild

ah ok, so the issue is static member overloads then.

m0rkeulv avatar Jun 14 '25 19:06 m0rkeulv

ah ok, so the issue is static member overloads then.

No, the problem does not only occur in internal access.

class Test {
    public function new() {
        var test2 = new Test2();

        test2.test("Hello");
        test2.test(42);
        test2.test(42, "Hello");
    }
}

class Test2 {
    public overload extern inline function test(a: String): Void {
        trace("a string");
    }

    public overload extern inline function test(a: Int): Void {
        trace("an int");
    }

    public overload extern inline function test(a: Int, b: String): Void {
        trace("both!");
    }
}

Image

barisyild avatar Jun 14 '25 19:06 barisyild

i have made a beta build that support some overload usage, it would be helpful if you tested it and let me know if it fixes your issues.

https://github.com/HaxeFoundation/intellij-haxe/releases/tag/pre-release%2F202506162026

m0rkeulv avatar Jun 16 '25 20:06 m0rkeulv

i have made a beta build that support some overload usage, it would be helpful if you tested it and let me know if it fixes your issues.

https://github.com/HaxeFoundation/intellij-haxe/releases/tag/pre-release%2F202506162026

Looks fixed.

barisyild avatar Jun 16 '25 21:06 barisyild

Seems to function as it should now 😎

mikaib avatar Jun 17 '25 11:06 mikaib

Heya, I did find a small bug. Currently it seems like extern (inline) overloads don't work on constructors.

mikaib avatar Jun 30 '25 15:06 mikaib

Image

class MyClass {
    public inline extern overload function new(a: Int) {}
    public inline extern overload function new(a: Int, b: String) {}
}

class Test {
    static function main() {
        var a = new MyClass(1);
        var b = new MyClass(1, "test");
    }
}

here is a sample, this gives:

Image

on the variable declaration of "b"

mikaib avatar Jun 30 '25 15:06 mikaib

i don't think that trick is allowed on constructors, so the error seems correct, but the declaration should probably show an error

https://try.haxe.org/#4D23379B

[ERROR] Test.hx:8: characters 26-34

 8 |     public inline extern overload function new(a: Int) {}
   |                          ^^^^^^^^
   | Invalid modifier: overload on constructor

[ERROR] Test.hx:9: characters 26-34

 9 |     public inline extern overload function new(a: Int, b: String) {}
   |                          ^^^^^^^^
   | Invalid modifier: overload on constructor

[ERROR] Test.hx:9: characters 5-69

 9 |     public inline extern overload function new(a: Int, b: String) {}
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   | Duplicate constructor

[ERROR] Test.hx:4: characters 32-38

 4 |         var b = new MyClass(1, "test");
   |                                ^^^^^^
   | Too many arguments

m0rkeulv avatar Jun 30 '25 16:06 m0rkeulv

Hmm, it does seem valid on an abstract though... This is very odd behaviour.

https://try.haxe.org/#F746A3E1

I wrote up the above minimal example as a demo, didn't think it being a class or abstract would change the behaviour

mikaib avatar Jun 30 '25 20:06 mikaib

I have added support for this in dev now, should be part of next release

m0rkeulv avatar Jul 04 '25 12:07 m0rkeulv

Awesome!

mikaib avatar Jul 05 '25 19:07 mikaib