HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.18k stars 655 forks source link

@:multiType abstract vs normal (non-static) @:to methods #10145

Closed nadako closed 3 years ago

nadako commented 3 years ago

The following fails with src/Main.hx:13: lines 13-15 : (this : I<C.T>) -> C.T should be (I<Unknown<0>>, Unknown<1>) -> Unknown<2>. Looks like the usual @:to conversion methods don't work for @:multiType abstracts.

interface I<T> {
    function get():T;
}

@:multiType(T)
abstract C<T>(I<T>) {
    public function new(value:T);

    @:to static function ofInt(_:I<Int>, value:Int):C<Int> {
        return null;
    }

    @:to function toT():T {
        return this.get();
    }
}

function main() {
    var x = new C(10);
    var y:Int = x;
}
Simn commented 3 years ago

I don't know if non-static @:to methods on @:multiType abstracts should even be allowed. This is a bit of a mess due to the special semantics these abstracts have.

I'll take a closer look, but my tentative recommendation is to work around this problem and not rely on @:multiType.

Simn commented 3 years ago

I've put in a fix for your example. Not super confident about it, so let me know if there are any problems. I would still love to just get rid of @:multiType and handle this properly, but that's a discussion for another day.

Simn commented 3 years ago

This has been addressed, so I'll close until somebody complains.