haxe icon indicating copy to clipboard operation
haxe copied to clipboard

[suggestion] @:forward.op for abstracts

Open Jarrio opened this issue 5 months ago • 2 comments

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

class Test {
	static function main() {
		var x = Example.a * 5;
		trace(x);
	}
}

enum abstract Example(Float) to Float {
	var a = 0;
	var b = 1;
}

The above results in:

[ERROR] Test.hx:3: characters 11-20 3 | var x = Example.a * 5; | ^^^^^^^^^ Example should be Int

something like this crops up often where the solution is to do var x = (Example.a:Float) * 5; or write the @:op's for the abstract

It would be nice if we could have a @:forward.op meta which in this particular situation would remove the need to have the to Float in a lot of cases

Jarrio avatar Jul 12 '25 08:07 Jarrio

I ran into this situation too, I have a cross target Int32 that keeps division as an Int, I operator overload all of the cases but using the abstract Int32 in another abstract in which I want to keep the operator overloads the same but have to write them all out is a pain.

Here is an example of trying to use the existing system to overload underlying operator overloads and running into problems:

import haxe.Int64;

function main() {
	var x:Int65 = 10i64;
	var y:Int65 = 5i64;
	trace(x + y);
}


abstract Int65(Int64) from Int64 to Int64 {
  @:op(A + B) static function add(a:Int65,b:Int65):Int65;
}
 6 |  trace(x + y);
   |        ^^^^^
   | Cannot add hl.I64 and hl.I64

      | For function argument 'v'

PXshadow avatar Jul 26 '25 07:07 PXshadow

I've ran into this issue before, specifically with a Reference<T> type which I specifically did not want to use Dynamic for because of type safety (even if it converts the type parameter to Dynamic anyway on hxcpp)

mikaib avatar Jul 26 '25 08:07 mikaib