`proc` and `func` uses static dispatch. To enable dynamic dispatch, `method` 
must be used instead: 
[https://nim-lang.org/docs/manual.html#multiminusmethods](https://nim-lang.org/docs/manual.html#multiminusmethods)

This appears to be working: 
    
    
    type
      BBTree*[K,V] = BBLeaf[K, V] # BBTree is a generic type with keys and 
values of types K, V
          ## `BBTree` is an opaque immutable type.
      BBLeaf[K,V] = ref object of RootObj
          key:   K                # the search key; must support the generic 
``cmp`` proc
          val:   V                # the data value associated with the key, and 
stored in a node
      BBNode[K,V] = ref object of BBLeaf
          vleft:  BBTree[K,V]      # left subtree; may be nil
          vright: BBTree[K,V]      # right subtree; may be nil
          vsize:  int              # the size of the (sub-)tree rooted in this 
node
    
    method size[K,V](n: BBLeaf[K,V]): int = return 1
    method left[K,V](n: BBLeaf[K,V]): BBTree[K,V] = discard
    method right[K,V](n: BBLeaf[K,V]): BBTree[K,V] = discard
    
    method size[K,V](n: BBNode[K,V]):  int = return n.vsize
    method left[K,V](n: BBNode[K,V]):  BBTree[K,V] = return n.vleft
    method right[K,V](n: BBNode[K,V]):  BBTree[K,V] = return n.vright
    
    
    Run

Reply via email to