I'm trying to do some compile time type introspection, however I'm not getting the results I expect. Here is a minimal version of what I'm trying to do:
# ---
import macros
type StructShape[T] = object
fields: seq[tuple[key, typename: string]]
macro getFields(struct: typedesc): seq[tuple[key, typename: string]] =
echo "Type declartaion is: ", struct.getTypeImpl.repr
@[]
proc getShape*(struct: typedesc): StructShape[struct] =
result.fields = getFields(struct)
# ---
type Foo = object
bar: string
baz: int
let shape = getShape(Foo)
I'm expecting the call to getTypeImpl to return the full AST of Foo's type declaration, but that isn't happening. Instead, I'm getting:
Type declartaion is: typeDesc[Foo]
I've tried this with 0.19.2 and 0.18.0
I did some deep diving and I've managed to answer my own question. This code gives me access to the type declaration:
echo "Type declartaion is: ", struct.getTypeImpl[1].getTypeImpl.repr
Though I couldn't tell you _why it needs to be like that.