let tp = getType(typedNode)
let isString = $(tp) == "string"
There are a few issues with this approach.
What I would like is something like
proc isT[T](n: NimNode): bool {. compileTime .} =
...
to be used like
let isString = isT[string](typedNode)
but I am not able to write the body of that procedure.
I have tried many variations, but I always run into the problem that T is a type, while n is a NimNode. I can get the AST of a variable of type T using a macro, but then I am one level of macro up and I am not able to get the type of n anymore
if x.gettype.typeKind == ntyString
Great, this is almost what I need! The only missing part, now, would be to be able to produce a typed node with a given type.
Say I have a type such as, in my case, TimeInfo and a typed node n. If I understand correctly I should be able to tell whether n is of type TimeInfo by doing
sameType(n, ???)
where ??? is a NimNode of type TimeInfo. The problem is that I do not know how to produce it! I have tried
template helper =
var x: TimeInfo
sameType(n, getAst(helper())[0][0][0])
which should compare the type of n with that of the variable x defined in the template. Unfortunately, it seems that x is not typed at this point. I am not sure how to produce a typed node given the type. I can do it by going inside one more level of macro, like this
proc ofTypeM(t: typed) =
# here `t` is typed of type `T`
discard
proc ofType(T: typedesc) =
var t: T
discard ofTypeM(t)
but then I do not know how to compare it with the type of n, which is only in the first level of macro, so to speak.
proc hasType(x: NimNode, t: static[string]): bool {. compileTime .} =
sameType(x, bindSym(t))
# then, inside a macro...
if field.hasType("TimeInfo"): # do something