I'm trying to get the AST for a type passed to a macro. The following works, but it requires a variable instead of a typedesc to be passed to the macro:
macro typetraitImpl(x: untyped): string =
newLit(getTypeImpl(x).treeRepr)
template typetrait(T: typedesc): string =
var x: T
typetraitImpl(x)
echo typetrait((int, int))
# TupleConstr
# Sym "int"
# Sym "int"
type X = (int, int)
echo typetrait(X)
# TupleConstr
# Sym "int"
# Sym "int"
I would have expected something like this to give the same result, but it does not:
macro typetrait(x: typedesc): string =
newLit(getTypeImpl(x).treeRepr)
echo typetrait((int, int))
# BracketExpr
# Sym "typeDesc"
# TupleConstr
# Sym "int"
# Sym "int"
type X = (int, int)
echo typetrait(X)
# BracketExpr
# Sym "typeDesc"
# Sym "X"
Is there any way to get the result from the first example without having an additional template?