If I've got a macro where I pass an ident as an argument, is it possible to read the signature of the proc? Here is an example of what I'm trying to do:
import macros
macro examine(ident: untyped) =
ident.expectKind(nnkIdent)
# My goal is to read the list of arguments right here, but
# the following line fails with the error: "node is not a symbol"
echo ident.getImpl
# ----
proc someProc[T](arg: T) =
discard
examine(someProc)
import std/macros
macro examine(ident: typed{sym}) =
# My goal is to read the list of arguments right here, but
# the following line fails with the error: "node is not a symbol"
echo ident.getImpl.treeRepr
# ----
proc someProc[T](arg: T) =
discard
examine(someProc)
If you want to look up a symbol you need to have typed ast, as ident s are just fancy strings labels. As it's easier I used the Nim constraints so it'll only take {sym} s.Awesome! Thank you
I might be pressing my luck, but is it possible to do this with concepts, too?
import macros
macro examine(ident: typed) =
echo ident.getImpl.repr
# ----
type Foo = concept f
proc someProc(arg: Foo) =
discard
examine(someProc)
Fails with:
tests/t_example.nim(13, 9) Error: 'someProc' doesn't have a concrete type, due to unspecified generic parameters.
Odd it works on explicit generics but not implicit, here is how you'd do it
import std/macros
macro examine(ident: typed) =
echo ident.getImpl.treeRepr
# ----
type Foo = concept f
proc someProc[T: Foo](arg: T) =
discard
examine(someProc)