I'm trying to make an implementation of a graph-like data structure in Nim. For
that I need object types which are referencing each other (each node needs to
have references to its edges, and each edge needs references to nodes it
connects).
As stated in the manual, it can be easily done, because Nim supports mutually
recursive types:
type
Node = ref object
someEdge: Edge
Edge = ref object
someNode: Node
var x = Node() # Works
echo repr(x)
Run
However, when I try to introduce generics here, it won't compile anymore:
type
GenericNode[E] = ref object
someEdge: E
GenericEdge[N] = ref object
someNode: N
type
Node = GenericNode[Edge]
Edge = GenericEdge[Node]
var x = Node() # Error: invalid type: 'Edge' in this context: 'Node' for
var
echo repr(x)
Run
In case of a non-mutually recursive type I get another error:
type
GenA[T] = ref object
field: T
type
A = GenA[A]
var x = A() # Error: object constructor needs an object type
echo repr(x)
Run
Am I doing something wrong here or generics are simply cannot be used in this
scenario?