Given the following code:
enum BasicColor
Red,
Green,
Blue
end
def max_out(color : BasicColor) : String
case color
when BasicColor::Red then "#ff0000"
when BasicColor::Green then "#00ff00"
when BasicColor::Blue then "#0000ff"
end
end
puts max_out(BasicColor::Red)
Currently, you get this error:
Error: method must return String but it is returning (String | Nil)
This of course makes no sense, because you annotated the method param as exactly BasicColor
(not BasicColor?
), and you handled all the cases, returning a non-nilable String
in every case. But because there’s no else
case that returns a String
(even though such a case would be unreachable), the compiler complains.
In short, the compiler is not aware that our case statement is exhaustive.
Is this something that’s on the roadmap for Crystal, or is it deemed out of scope for the compiler itself?