void test(int p)
{
final switch (p)
{
case 42:
break;
}
}
void main()
{
test(0);
}
dmd -betterC test.d
/dlang/dmd/linux/bin64/../../src/druntime/import/core/exception.d(584): Error: Cannot use throw statements with -betterC
/dlang/dmd/linux/bin64/../../src/druntime/import/object.d(4180): Error: template instance `core.exception.__switch_errorT!()` error instantiating
This is caused by a `throw` statement in the implementation of `__switch_errorT` in druntime. It should probably be an `assert`.
Comment #1 by greensunny12 — 2018-08-15T23:38:04Z
> It should probably be an `assert`.
Agreed! `final switch` is actually usable in `nothrow` code (see e.g. https://run.dlang.io/is/mjVbPr), s.t. there's no reason to throw an exception.
Also programming errors are typically handled by `assert`.
Comment #3 by pro.mathias.lang — 2018-08-16T01:09:05Z
Wasn't the plan to make this a compile-time error, or does it needs to be a runtime error ?
Comment #4 by slavo5150 — 2018-08-16T01:59:23Z
I'm not sure how to make it a compile-time error. For example the code could be...
void test(int p)
{
final switch (p)
{
case 42:
break;
}
}
void main()
{
import core.stdc.stdlib;
test(rand());
}
How can we predict what `rand()` will return. And I don't think it's practical to enumerate all 2^32 possibilities for `int`. Though maybe I'm missing something.