Oh, you did not declare the types as `ref`. Polymorphism does not work with 
plain objects because there is no place to store the virtual tables. Try this:
    
    
    type Uno = ref object of RootObj
        x: float
    
    type Due = ref object of Uno
    type Tre = ref object of Uno
        y: float
    
    proc newDue(x: float): Due = Due(x: x)
    proc newTre(x, y: float): Tre = Tre(x: x, y:y)
    
    method `$`(x: Uno): string {.base.} =
      system.`$`(x[])
    
    method `$`(x: Due): string =
      system.`$`(x[])
    
    method `$`(x: Tre): string =
      system.`$`(x[])
    
    var appo = 1.2
    var caz = (if appo>1: newTre(0.5, 2.0) else: newDue(1.0))
    echo caz
    
    
    Run

Reply via email to