i'd recommend you read through the nim tutorial and manual, the sections on 
variants in the 
[tutorial](https://nim-lang.org/docs/tut2.html#object-oriented-programming-object-variants)
 and [manual](https://nim-lang.org/docs/manual.html#types-object-variants) in 
particular.
    
    
    type NodeKind = enum
      Leaf,
      Branch
    type Node = object
      case kind: Nodekind
      of Leaf:
        value: int # or whatever
      of Branch:
        kids: seq[Node]
    
    
    Run

is very typical Nim code, and how Nim itself is implemented.

to handle variants in a proc you also use the case statement
    
    
    import std/[sequtils]
    
    proc nd(i:int):Node = Node(kind: Leaf, value: i)
    proc nd(n:Node):Node = n
    proc nd(x:varargs[Node]):Node = Node(kind: Branch, kids: @x)
    
    proc sum(node: Node):int =
      case node.kind
      of Leaf:
        return node.value
      of Branch:
        return foldl(node.kids,a+sum(b),0)
    
    let n = nd(nd(3),nd(4),nd(nd(5),nd(6)))
    
    assert sum(n)==18
    
    
    Run

Reply via email to