# Why does this macro usage err?

**URL:** <https://forum.crystal-lang.org/t/why-does-this-macro-usage-err/1433>\
**Category:** Help & Support\
**Created:** [December 2, 2019, 7:56pm UTC](https://forum.crystal-lang.org/t/why-does-this-macro-usage-err/1433 "2019-12-02T19:56:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fxn](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fxn/32/1128_2.png) [@fxn](https://forum.crystal-lang.org/u/fxn)\
**Post date:** [December 2, 2019, 7:56pm UTC](https://forum.crystal-lang.org/t/why-does-this-macro-usage-err/1433/1 "2019-12-02T19:56:34Z")

</div>

I founded this case when working on today’s Advent of Code.

The gist of the problem is

```crystal
macro m(i)
  array[{{i}}]
end

array = [0]
p m(0) # => 0
m(0) = 1 # => Error: unexpected token: =

```

I guess it might be something related to generating well-formed expressions (fine in `p m(0)`) plus the fact that the `=` there is syntactic sugar. Which is the actual explanation?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [December 2, 2019, 8:05pm UTC](https://forum.crystal-lang.org/t/why-does-this-macro-usage-err/1433/2 "2019-12-02T20:05:56Z")

</div>

If I had to guess, the syntax is being checked before expanding the macro. The same thing happens if you do like.

```auto
def foo(val)
  val
end

foo(1) = 7

```

Since you can’t change the value of a method call. @asterite can probably speak more on the internals.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [December 2, 2019, 9:17pm UTC](https://forum.crystal-lang.org/t/why-does-this-macro-usage-err/1433/3 "2019-12-02T21:17:16Z")

</div>

Yeah, that’s the reason. You can’t assign to something returned by a method, and I never thought of using a macro like that.

It could be an enhancement, though… but it’ll have to be discussed to see whether it makes sense as an entire feature.

Maybe the reason is that on the left-hand side of an assignment you can have very specific things: a constant (`Foo`), an instance variable or a local variable. We could allow calls but then they’ll always have to resolve to a macro, and that macro should expand to those things above (or another macro, recursively).

Another problem:

```crystal
Foo.bar = 1

```

That calls `Foo.bar=(1)`. But what if `bar` is a macro? Should `Foo.bar` expand that macro and then assign `1` to it? I think this is where it can become really confusing and the syntax is already clashing.

So I’d say because of all of the above there’s a very little chance that we’ll end up implementing this.

---

<div class="post-metadata">

**Author:** ![fxn](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fxn/32/1128_2.png) [@fxn](https://forum.crystal-lang.org/u/fxn)\
**Post date:** [December 2, 2019, 9:42pm UTC](https://forum.crystal-lang.org/t/why-does-this-macro-usage-err/1433/4 "2019-12-02T21:42:15Z")

</div>

Gotcha, thanks!
