I create a type in a macro which depends on other type ( i create the World
type) The issue is that the world type depends on other types (Store and
Entity) which are in the same typeblock. It seems that i cannot call the create
macro inside the type block to create the type. When i call the gen macro above
or below the type block there are types missing. Any idea how i can do this?
type
# i want to create the world type
World = ref object
guids: GUID
storeHealthAble: Store[CompHealthAble]
storeDrawAble: Store[CompDrawAble]
storeNameAble: Store[CompNameAble]
Entity = object
id: GUID
world: World
Store[T] = Table[Entity,T]
# Following for completeness.
Component = enum
DrawAble, HealthAble, NameAble
Run
the type creation macro which creates World:
macro genWorld() =
var typeTree = newNimNode(nnkRecList)
typeTree.add nnkIdentDefs.newTree(
newIdentNode("guids"),
newIdentNode("GUID"),
newEmptyNode()
)
for component in Component:
typeTree.add nnkIdentDefs.newTree(
newIdentNode("store" & $component ),
nnkBracketExpr.newTree(
newIdentNode("Store"),
newIdentNode("Comp" & $component)
),
newEmptyNode()
)
result = nnkStmtList.newTree(
nnkTypeSection.newTree(
nnkTypeDef.newTree(
newIdentNode("World"),
newEmptyNode(),
nnkRefTy.newTree(
nnkObjectTy.newTree(
newEmptyNode(),
newEmptyNode(),
typeTree
)
)
)
)
)
Run