type
        IntOrString = int | string
    
    template doSth*(x: IntOrString): untyped =
        when x is int:
            echo $(x + 1)
        else:
            echo $(x & "1")
    
    when isMainModule:
        doSth(2)        # prints 3
        doSth("X")      # prints X1
        
        var myBool = true
        
        if myBool:      # this obviously works as well
            doSth(2)
        else:
            doSth("X")
        
        ##################
        doSth(if myBool: 2 else: "X") # got string, but expected int
        
        # let's trick the compiler
        var param: IntOrString =
            if myBool: 2            # got string, but expected int
            else: "X"
        
        var param2: int =           # but a non-generic variable
            if myBool: 2            # works fine
            else: 3
        
        # let's try to trick the compiler again
        var param3: IntOrString     # invalid type IntOrString for var
    
    
    Run

Is there any way to be able to do something like `doSth(if myBool: 2 else: 
"X")`, without being forced to repeat the whole thing over and over again?

P.S. obviously the issue is about a much more complex construct, but apparently 
I cannot seem to make even this one "work".

Reply via email to