Closed
Description
While trying to find an alternative to be able to slice with _
like a[1, _, 0]
without having to define my own _
I stumbled upon the following:
History:
- In procs with generic return type, identifier validity is checked before macro replacement Generics proc + macros: identifier resolution happens before macros #6387
- There is a request to disallow the
_
identifier except for tuple destructuring It's possible to use a single underscore as an enum value identifier (0.17.3) #7171
So I tried to inject mixin _
in a block expression before the identifier resolution happen.
Unfortunately this works in generic procs but not in normal proc.
Test case:
import macros
type CustomSeq*[T] = object
data*: seq[T]
macro `[]`*[T](s: CustomSeq[T], args: varargs[untyped]): untyped =
## The end goal is to replace the joker "_" by something else
result = newIntLitNode(10)
proc foo1(): CustomSeq[int] =
result.data.newSeq(10)
# Doesn't work
echo ((block:
# mixin _ # invalid expression
result[_]
))
echo foo1()
proc foo2[T](): CustomSeq[T] =
result.data.newSeq(10)
# works fine with generic return type
echo ((block:
mixin _ # Works
result[_]
))
echo foo2[int]()