# Abstract macro op overloading

**URL:** https://community.haxe.org/t/abstract-macro-op-overloading/3674
**Category:** Haxe
**Created:** [September 7, 2022, 10:54am UTC](https://community.haxe.org/t/abstract-macro-op-overloading/3674 "2022-09-07T10:54:36Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![filt3rek](https://community.haxe.org/user_avatar/community.haxe.org/filt3rek/32/2613_2.png) [@filt3rek](https://community.haxe.org/u/filt3rek)
#### Post date: [September 7, 2022, 10:54am UTC](https://community.haxe.org/t/abstract-macro-op-overloading/3674/1 "2022-09-07T10:54:36Z")

</div>

Hej,

I played with abstract and macro, so I get a macro function for `@:op(A.B)`.  
It seemed to work fine, I suppose an operator macro function is like a static function and the first argument (I thought) it was the object on which the function apply (like using macro with static extension…)  
But I realized that with a thing like `myobj.foo += 5`, the first argument of the macro function is no longer the object but another thing.  
So my question is : Does anyone know exactly what is the 1st argument of an op overloading macro function please ? And why in the case of `myobj.foo += 5` it becomes something else please ?

Here’s a complete example : [Try Haxe !](https://try.haxe.org/#e48533D6) (Take a look at Compiler Output please)

Here’s a fragment :

```haxe
@:op(A.B) macro function set(obj:haxe.macro.Expr, prop:haxe.macro.Expr, val:haxe.macro.Expr) {
		trace(Context.getTypedExpr(Context.typeExpr(obj)).toString(), prop.toString(), Context.getTypedExpr(Context.typeExpr(val)).toString());
}

```

```haxe
mydynobj.bar = "barry"; // macro trace : mydynobj,"bar","barry"
mydynobj.foo = 3; // macro trace : mydynobj,"foo",3
mydynobj.foo += 5; // macro trace : Test.ADyn_Impl_.get(mydynobj, "foo") + 5,"foo",Test.ADyn_Impl_.get(mydynobj, "foo") + 5

```

As you can see for the 2 first traces obj seems to be the object but in the 3rd, it becomes yet a combination … Maybe I just don’t know how the += op works on compiler side ? …

Thanks for reading !

---

<div class="post-metadata">

### Author: ![Ilir-Liburn](https://avatars.discourse-cdn.com/v2/letter/i/82dd89/32.png) [@Ilir-Liburn](https://community.haxe.org/u/Ilir-Liburn)
#### Post date: [September 8, 2022, 1:32pm UTC](https://community.haxe.org/t/abstract-macro-op-overloading/3674/2 "2022-09-08T13:32:07Z")

</div>

Hello,

that seems to be wrong, or we don’t understand how is it supposed to work. By simply returning obj as expression generates nothing, while returning binop explicitly produces error

```haxe
return { expr:	EBinop(op,Context.getTypedExpr(e1),Context.getTypedExpr(e2)), pos: Context.currentPos() }
// MyTest.hx:88: characters 3-20 : Class<MyTest> has no field ADyn_Impl_

```

[https://try.haxe.org/#8419eEE8](https://try.haxe.org/#8419eEE8)

---

<div class="post-metadata">

### Author: ![filt3rek](https://community.haxe.org/user_avatar/community.haxe.org/filt3rek/32/2613_2.png) [@filt3rek](https://community.haxe.org/u/filt3rek)
#### Post date: [September 8, 2022, 3:46pm UTC](https://community.haxe.org/t/abstract-macro-op-overloading/3674/3 "2022-09-08T15:46:29Z")

</div>

Hej,

I don’t know why you’re switching against TBinop, in fact my question is exactly why obj becomes that, or in other words and with a minimalistic example we get that : [Try Haxe !](https://try.haxe.org/#B33dD5ED) (Take a look at Compiler Output)

```haxe
import haxe.macro.Expr;
import haxe.macro.Context;

using haxe.macro.Tools;

abstract Foo(Dynamic) from Dynamic to Dynamic {
	@:op(A.B) function get(prop:String) {
		return Reflect.field(this, prop);
	}

	@:op(A.B) macro function set(obj:Expr, prop:Expr, val:Expr) {
		trace(Context.getTypedExpr(Context.typeExpr(obj)).toString(), prop.toString(), Context.getTypedExpr(Context.typeExpr(val)).toString());
		return macro $val;
	}
}

class Test {
	static function main() {
		var foo:Foo = {
			n: 5
		};
		foo.n = 4;	// compiler trace => foo,"n",4
		foo.n += 4;	// compiler trace => Test.Foo_Impl_.get(foo, "n") + 4,"n",Test.Foo_Impl_.get(foo, "n") + 4 => So "obj" = Test.Foo_Impl_.get(foo, "n") + 4 ???
	}
}

```

In my mind `obj += X` equals `obj = obj + X` so `obj` should always be `obj` and not become another thing.

---

<div class="post-metadata">

### Author: ![Ilir-Liburn](https://avatars.discourse-cdn.com/v2/letter/i/82dd89/32.png) [@Ilir-Liburn](https://community.haxe.org/u/Ilir-Liburn)
#### Post date: [September 8, 2022, 3:59pm UTC](https://community.haxe.org/t/abstract-macro-op-overloading/3674/4 "2022-09-08T15:59:43Z")

</div>

Hello,

I’m switching against TBinop because that’s what we get from Eval. Why we are getting this: that’s a question for the Haxe team.

[Last and final test](https://try.haxe.org/#DcCB5AA5)

---

<div class="post-metadata">

### Author: ![filt3rek](https://community.haxe.org/user_avatar/community.haxe.org/filt3rek/32/2613_2.png) [@filt3rek](https://community.haxe.org/u/filt3rek)
#### Post date: [September 8, 2022, 4:14pm UTC](https://community.haxe.org/t/abstract-macro-op-overloading/3674/5 "2022-09-08T16:14:05Z")

</div>

Yes my main question here is why we get this, so I’ll be glad if someone from Haxe team could answer that.  
Maybe `A+=B` op is not equal to `A=A+B` or something else, I don’t know…

---

<div class="post-metadata">

### Author: ![thispolo](https://community.haxe.org/user_avatar/community.haxe.org/thispolo/32/1909_2.png) [@thispolo](https://community.haxe.org/u/thispolo)
#### Post date: [September 9, 2022, 6:28am UTC](https://community.haxe.org/t/abstract-macro-op-overloading/3674/6 "2022-09-09T06:28:00Z")

</div>

```haxe
Maybe A+=B op is not equal to A=A+B or something else, I don’t know…

```

This depends on the target you’re transpiling to, and whether or not the targetted compiler can resolve that syntax. For example, Lua has no concept of `+=` whereas JavaScript does. For this reason, I imagine Haxe will always see `+=` as `Binop(OpAssignOp(OpAdd), expr1, expr2)` so that each individual transpiler can interpret that expression according to its needs. (Edit: I also believe for certain types, Haxe can optimise out this expression entirely and replace it with the expected result at compile time).

In other words, never assume `A += B` resolves to `A = A + B` after compilation. I suppose this is why explicitly writing out `A = A + B` is why you don’t see a compiler error. Not yet sure why you’re seeing a different type because of it 🙁

---

<div class="post-metadata">

### Author: ![filt3rek](https://community.haxe.org/user_avatar/community.haxe.org/filt3rek/32/2613_2.png) [@filt3rek](https://community.haxe.org/u/filt3rek)
#### Post date: [September 9, 2022, 7:13am UTC](https://community.haxe.org/t/abstract-macro-op-overloading/3674/7 "2022-09-09T07:13:01Z")

</div>

Thanks for your answer.  
Yes, and here it occurs on Haxe AST not on the generated code, so I think the target has nothing to do but it’s interesting to know that some languages don’t have +=, I didn’t know

---

<div class="post-metadata">

### Author: ![Ilir-Liburn](https://avatars.discourse-cdn.com/v2/letter/i/82dd89/32.png) [@Ilir-Liburn](https://community.haxe.org/u/Ilir-Liburn)
#### Post date: [September 9, 2022, 1:05pm UTC](https://community.haxe.org/t/abstract-macro-op-overloading/3674/8 "2022-09-09T13:05:53Z")

</div>

> [@thispolo](#):
>
> This depends on the target you’re transpiling to, and whether or not the targetted compiler can resolve that syntax

That’s reasonable. But brings more questions: why do we get

```haxe
Test.ADyn_Impl_.get(mydynobj, "foo") + 5,"foo",Test.ADyn_Impl_.get(mydynobj, "foo") + 5

```

instead of

```haxe
mydynobj,"foo",Reflect.field(mydynobj, "foo") + 5

```

?

[Macro callback (see compiler output)](https://try.haxe.org/#130A2632)

---

<div class="post-metadata">

### Author: ![filt3rek](https://community.haxe.org/user_avatar/community.haxe.org/filt3rek/32/2613_2.png) [@filt3rek](https://community.haxe.org/u/filt3rek)
#### Post date: [September 9, 2022, 1:19pm UTC](https://community.haxe.org/t/abstract-macro-op-overloading/3674/9 "2022-09-09T13:19:50Z")

</div>

I’ve put an issue on Haxe Github [Abstract Macro @:op(A.B) overloading weird · Issue #10793 · HaxeFoundation/haxe · GitHub](https://github.com/HaxeFoundation/haxe/issues/10793)  
I hope they won’t kick me out 🤣
