The Crystal Programming Language Forum

Exhaustive case

Is there some way we can make case be exhaustive, and force an else in case the compiler can’t prove that?

Ref: https://github.com/crystal-lang/crystal/issues/8001

This is the only thing that really bothers me about Crystal. Having to write else raise "Bug: can't happen" is really, really annoying and makes the language look unprofessional.

I know Crystal has several bugs, many related to generics, but these are bugs. They will be fixed, most probably without making a big change in the language.

However, case not being exhaustive is not a bug. It doesn’t need to be fixed. It needs to change.

We need to make the language better and more type safe. Requiring hash lookups to match key types is one way to make that happen. Making case be exhaustive, like in many other languages, is another way to make that happen.

I already sent a PR with that? Could we move forward with that? What’s the fear?

If I’m in charge of that, there’s no fear about losing time on it because I will be working on it and I’m sure @straight-shoota , @RX14 and other non-Manas people will be able to review it and move it forward.

Thoughts?

11 Likes

Yeah, I really don’t understand why we didn’t already merge #8424. There was a objection late in the process, but the discussion was not picked up again. I’d be fine with merging that PR as is.

Yeah case statements have been one of the few places where I’ve thought “what?” with crystal…

other examples (though they deal with Enum’s, somewhat related?)

https://groups.google.com/d/msg/crystal-lang/YJbUG1lQP3Q/A2sJzUQACgAJ