Allow omitting else for CIF switch expression over more types
Currently, for the following CIF specification:
enum E = a, b, c;
input E x;
alg int y = switch x: case a: 0 case b: 1 case c: 3 end;
We get the following type checker error: The switch is missing an "else".
Clearly, this switch is complete, and does not need the else
, but the type checker does not seem to know this.
You can work around this:
enum E = a, b, c;
input E x;
alg int y = switch x: case a: 0 case b: 1 else /* c */ 3 end;
But that is not as nice.
We already allow omitting the else
for a switch over an automaton, if all locations are present. I propose to do the same for switch expressions over enum-typed values, where there is a case for each of the enum's literals. As some expressions may not be statically evaluable, we would only allow omitting the else
if the type checker can statically determine that all the enum's literals are present.
We could also consider generalizing this. For a bool
-typed value, having true
and false
is also sufficient. For a tuple(bool, E)
, we would have 6 possible values. Etc. This would require a function to check a type against a set of values, to see if all values of the types domain are covered. Not sure whether we should start with this generalization immediately, or just implement it for enums, where it seems most useful.