Thanks! I tried the "derived" version earlier, but with stack allocated 
objects, and I did not manage to get dispatch working correctly at callsite. 
But after changing to ref values, it works. Is ref values required to 
differentiate sub-types?
    
    
    type
      # If I remove ref from base, A and B
      # no output is printed
      
      Base = ref object of RootObj
      
      A = ref object of Base
        a_value: int
      
      B = ref object of Base
        b_value: int
    
    proc test(arg: int): Base =
      if arg < 0: return A(a_value: arg)
      else:       return B(b_value: arg)
    
    let a = test(10)
    
    if a of A:
      echo "Got A ",A(a).a_value
    if a of B:
      echo "Got B ",B(a).b_value
    

Reply via email to