I am starting to dabble into type introspection. I have the following test program:
import
sugar,
macros
type
MyInfo* = object
fname*: string
cname*: string
ctype*: string
var
myInfoAttrs: seq[string]
static:
myInfoAttrs = collect(newSeq):
for sym in getType(MyInfo)[2]: $sym
echo myInfoAttrs
I would like to _use the static sequence myInfoAttrs in my normal code, but I get "Error: cannot evaluate at compile time: myInfoAttrs". So I do something wrong here.
Also, is there a way to access attributes/fields of MyInfo via dynamic strings? - I assume not, because Nim is statically compiled, but maybe there's a trick with macros?
myInfoAttrs is a runtime address, you cannot use it at compile time (in the static block). You can still create the seq at compile time:
import
sugar,
macros
type
MyInfo* = object
fname*: string
cname*: string
ctype*: string
var
myInfoAttrs: seq[string]
myInfoAttrs = static:
collect(newSeq):
for sym in getType(MyInfo)[2]: $sym
echo myInfoAttrs
Thanks @Hlaaftana! When I see your change, it is obvious, but I was banging my head against the keyboard instead...
And I guess, fieldPairs is exactly what I was looking for!