I want to get a proc's name, from inside a proc, at compile time, so as to use it as a string.
Is it possible?
Hmm.... not sure how it works, or if it is what I need.
Let me elaborate a bit.
Let's say we have this:
proc someProc(x: int) =
let z = __proc___ ## <--- this should return the caller's name, hence 'someProc'
Not sure if there's a way that the compiler can do this currently, but you can do this with a macro like so:
import macros
macro injectProcName(procDef: untyped): untyped =
procDef.expectKind(nnkProcDef)
let
procName = procDef[0].toStrLit
procNameId = ident("procName")
let pnameDef = quote do:
let `procNameId` = `procName`
procDef.body.insert(0, pnameDef)
return procDef
proc test() {.injectProcName.} =
echo procName
test()
# Output:
# test