> if string|int is not a valid return type

`string | int` is a valid return type, in particular in your case if you know 
statically the boolean you can use this (which is a valid alternative to the 
variant where you assume you know stuff at compile time):
    
    
    proc test(b: static[bool]) : int|string =
      when(b): 1
      else: "hello"
    
    echo test true
    echo test false
    
    
    Run

> shouldn't the compiler emit an error

it does emit the error: `Error: type mismatch: got 'string' for '"hello"' but 
expected 'int literal(1)'`. Since the function `test` is a generic (the return 
type being a type class means that for every return type a new proc will be 
generated), the specific proc is generated when it is instantiated. This 
happens when you call `echo test true` and in that case the compiler realizes 
that the generic proc definition is not valid (in principle in this case it 
could have realized it earlier but it is not true in general) and throws the 
correct error: the if expression has `int` as first expression returned, so all 
other branches must have an `int`, I am expecting an int (which I derived being 
an int from a literal 1), and now I find an "hello"

Reply via email to