I stumbled over a problem when trying to define unary operators as macros.
The following example defines a macro which converts an identifier to a string:
import macros
macro M(s: untyped): string =
result = toStrLit(s)
echo M(foo)
which gives "foo" as output.
But I can't define the macro as a unary operator. If I try the following
import macros
macro `@`(s: untyped): string =
result = toStrLit(s)
echo @foo
I get: undeclared identifier: 'foo'
I wonder what's the reason for this? Is it because there is a system-defined operator that gets priority? Is there a way around that?
I noticed that binary macros as operators do work, at least when the first argument is not untyped.
This works for me so I guess it's because you are overloading @ which is magic
import macros
macro `@!<>`(s: untyped): string =
result = toStrLit(s)
echo @!<>foo
Furthermore I suppose it's because @ accepts generic params and so you are in the meta issue of Early symbol resolution with macros + generics