I have this macro that seems to work in older versions of Nim. What am I missing? Nim complains of inconsistent typing for reintroduced symbol 'paramsGlobal' previous type was: object; new type is: object
MRE below, where the macro creates a new type, then instances that type with var. My actual macro code is shorter, but using the output of dumpAstGen reduces possible mistakes.
import macros
macro DefineAndInstance =
nnkStmtList.newTree(
nnkTypeSection.newTree(
nnkTypeDef.newTree(
nnkPragmaExpr.newTree(
nnkPostfix.newTree(
newIdentNode("*"),
newIdentNode("GlobalParams")
),
nnkPragma.newTree(
newIdentNode("inject")
)
),
newEmptyNode(),
nnkObjectTy.newTree(
newEmptyNode(),
newEmptyNode(),
nnkRecList.newTree(
nnkIdentDefs.newTree(
nnkPostfix.newTree(
newIdentNode("*"),
newIdentNode("prm")
),
newIdentNode("float"),
newEmptyNode()
)
)
)
)
),
nnkVarSection.newTree(
nnkIdentDefs.newTree(
nnkPragmaExpr.newTree(
nnkPostfix.newTree(
newIdentNode("*"),
newIdentNode("paramsGlobal")
),
nnkPragma.newTree(
newIdentNode("inject")
)
),
newEmptyNode(),
nnkObjConstr.newTree(
newIdentNode("GlobalParams"),
nnkExprColonExpr.newTree(
newIdentNode("prm"),
newLit(0.0)
)
)
)
)
)
expandMacros DefineAndInstance
Elegantbeef helped me out to determine a smaller actual MRE.
macro DefineAndInstance =
genast():
type MyObject = object
var myParams* = MyObject()
Error
inconsistent typing for reintroduced symbol 'myParams`gensym1': previous type was: object; new type is: object
The solution is... Don't use expandMacros and the error goes away.