The real question is: What would anyone gain from allowing this code to compile, when all you have to do is to write
when T is bool:
return "bool"
elif T is float:
return "float"
Your code does not get shorter, nor can you do things that would not be possible otherwise. So, allowing your code to compile would only place an additional burden on the compiler without any return of investment. My code also makes the intention clearer: Either do one thing or the other, while your code says Maybe do this, and maybe do that.
I don't know the implementation details, but I would assume that it is not trivial for the compiler to prove that T is bool
and T is float
are disjoint.
Edit: Proof that there is no benefit
Assume our body starts with
when T is bool: return "bool"
Obviously, if T is bool
evaluates to true
at compile time, an unconditional return
statement gets compiled and there may not be statements after it. That is to say, all statements coming after the when
block must be swallowed at compile time unless the above condition is false
.
I believe there are basically two different statements that may be swallowed at compile time: other when
statements and calls to templates or macros (they may return an empty AST node). I already showed that a succeeding when
may be replaced by an elif
without any loss of expressiveness. Now let's look at a macro or template call:
when T is bool: return "bool"
call(T)
This can easily be fixed by rewriting it as:
when T is bool: return "bool"
else: call(T)
I admit that we have more code here, but I do not see any real gain we would get from leaving out the else:
.
If you are still not convinced, I invite you to give example code where you think you would have an actual benefit, so that we can discuss it.