Nim: Add pragma for disabling enforced exhaustive case coverage

Created on 19 Apr 2020  路  5Comments  路  Source: nim-lang/Nim

Problem

Enforcing exhaustive coverage in case statement is an awesome feature, but sometimes you don't know if you have covered all cases or not. This creates the following situation where there is always a possibility to get an error:

  • If you don't add an else: discard statement -> not all cases are covered
  • If you do add an else: discard statement ----> invalid else, all cases are already covered

This of course is no problem during the general development case, because the compiler will just inform you whether you are covering all cases or not, which is great. In case you are generating code though, getting such compile errors when you know your generated code is correct can be bothersome.

Possible solutions

  1. Introduce a pragma for disabling the check of exhaustive coverage. Possible names:

    • {.dirtyCase.}
    • {.lenientCase.}
  2. Change Error: invalid else, all cases are already covered into a warning.

Most helpful comment

I think this

discard statement ----> invalid else, all cases are already covered

should be made a warning instead of an error.
Then no new language features are required.

All 5 comments

Why not just generate the else: clause in your bothersome cases?

I have no specific bothersome cases. The codegen must be generic because I have no way to distinguish between when I should and when I should not generate it.

I think this

discard statement ----> invalid else, all cases are already covered

should be made a warning instead of an error.
Then no new language features are required.

Oh, @Clyybber actually your solution sounds great!

I'm in favor of this after talking to Clyybber. It's more ergonomic for the programmer.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tronic picture Tronic  路  3Comments

zaxebo1 picture zaxebo1  路  4Comments

capocasa picture capocasa  路  3Comments

timotheecour picture timotheecour  路  3Comments

juancarlospaco picture juancarlospaco  路  3Comments