The generics approach seem a a little less straightforward and makes 
constructing a Parent a little more awkward (see below):
    
    
    type
      Parent = ref object of RootObj
        parentField: int
      
      Child = ref object of Parent
        childField: string
    
    block:
      # Using generics
      proc newParent[T: Parent](value = 123): T =
        new result
        result.parentField = value
      
      proc newChild(value: string): Child =
        result = newParent[Child]321
        result.childField = value
      
      var
        parent = newParent[Parent]() # *** Too bad can't default Parent ***
        child = newChild "ABC"
      
      echo "parent parentField: ", parent.parentField
      echo "child parentField : ", child.parentField
      echo "child childField :  ", child.childField
    
    
    
    Run

Reply via email to