import msgpack
type
NodeKind = enum # the different node types
nkInt, #a leaf with an integer value
nkFloat, #a leaf with a float value
nkString, #a leaf with a string value
nkAdd, #an addition
nkSub, #a subtraction
nkIf #an if statement
Node = ref NodeObj
NodeObj = object
case kind: NodeKind # the kind field is the discriminator
of nkInt: intVal: int
of nkFloat: floatVal: float
of nkString: strVal: string
of nkAdd, nkSub:
leftOp, rightOp: Node
of nkIf:
condition, thenPart, elsePart: Node
var
aUnion = Node(kind:nkInt, intVal:22)
anotherUnion = Node(kind:nkInt, intVal:22)
s = pack(aUnion)
unpack(s, anotherUnion)
echo anotherUnion
...
type mismatch: got (Stream, NodeKind)
but expected one of:
msgpack.unpack(data: string, val: var T)
msgpack.unpack(s: Stream, val: var T)