operator overloading in generic function

49 views
Skip to first unread message

Adrian Veith

unread,
Sep 3, 2014, 4:56:21 AM9/3/14
to haxe...@googlegroups.com
Hi,

I want that an overloaded operator is called in a generic function like this:

abstract Funny(Int) from Int to Int {
 
inline public function new(i: Int) this = i;
   
@:op(A == B) static inline public function eq1(a:Funny,b: Funny) return a != b;
}


class Foo<T> {
   
public static function test<T>(A:T,B:T): Bool {
       
return A == B;  
   
}
}


class Test {
   
   
static function main() {
       
var a = new Funny(1);
       
var b = new Funny(2);
        trace
( a==b );          // here the overloaded op is called
        trace
(Foo.test(a, b )); // here not
   
}
}


what am I doing wrong ?

cheers Adrian.

Juraj Kirchheim

unread,
Sep 3, 2014, 6:56:53 AM9/3/14
to haxe...@googlegroups.com
You cannot do that because of how type parameters work. When the
compiler sees T, it has no clue that that may actually be an abstract
with its own operators. And since abstracts exist at compile time
only, the operator you defined cannot be resolved at runtime. The one
being used is the one defined on the underlying data type, and that's
integer equality in this case.

Adding `@:generic` metadata to `test` may cause the compiler to
generate a version for each type that will use the actual operator,
but I wouldn't count on it.

Alternatively you can use a macro:

class Foo<T> {
macro public static function test(a,b) {
return macro @:pos(haxe.macro.Context.currentPos) $a == $b;
}
}

Best,
Juraj
> --
> To post to this group haxe...@googlegroups.com
> http://groups.google.com/group/haxelang?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Haxe" group.
> For more options, visit https://groups.google.com/d/optout.

Adrian Veith

unread,
Sep 3, 2014, 8:50:37 AM9/3/14
to haxe...@googlegroups.com
Hi Juraj,

thanks for the explanation, but I am not very happy about this. Your
macro solution works (and helps me for what I want to do), but the
generic way not (@:generic does not work either). I think this is a bug
- at least a source of potential bugs.

cheers, Adrian.

Juraj Kirchheim

unread,
Sep 3, 2014, 9:09:25 AM9/3/14
to haxe...@googlegroups.com
Yes, I see why that is counter-intuitive. The actual issue really lies
with the fact that `==` is defined for all types. If you try `>` the
compiler will actually reject it. Unfortunately I don't think the way
`==` works can change before Haxe 4 sees the day of light, and that is
rather far ahead ;)

In the mean time, I suggest you try your luck on the Haxe issue
tracker to make @:generic work the way you would want it to. It seems
a reasonable request to me.

Best,
Juraj

Adrian Veith

unread,
Sep 3, 2014, 12:37:26 PM9/3/14
to haxe...@googlegroups.com
oh no - the macro solution works only for `==`

generics are instantiated to early, that's really bad. Other languages
are better in this.

cheers, Adrian.
Reply all
Reply to author
Forward
0 new messages