Change the last line to `echo caz.repr` and you'll see why:
    
    
    Uno(x: 0.5)
    
    
    Run

What other type could it have? `appo` is tested at runtime, and both branches 
of the conditional have to return the same type because the conditional itself 
is an expression with a single type. Although the `y` data is still there, it 
has to be retrieved with a runtime check:
    
    
    echo caz.Tre.repr  # Tre(y: 4.940656458412465e-324, x: 0.5)
    
    
    Run

which fails at runtime if that's invalid:
    
    
    var appo = 0.5
    var caz = (if appo>1: newTre(0.5, 2.0) else: newDue(1.0))
    echo caz.repr      # Uno(x: 1.0)
    echo caz.Tre.repr  # Error: unhandled exception: invalid object conversion 
[ObjectConversionDefect]
    
    
    Run

And how about a compile-time test of compile-time data?
    
    
    const appo = 0.5
    var caz = (when appo>1: newTre(0.5, 2.0) else: newDue(1.0))
    echo caz.repr
    echo caz.Tre.repr  # Error: type mismatch: got 'Due' for 'caz' but expected 
'Tre = object'
    
    
    Run

You get a compile-time error there, because `caz` is no longer an `Uno`, 
because the compile-time branches really can return different types. With the 
last line commented:
    
    
    Due(x: 1.0)
    
    
    Run

Reply via email to