removing default case requirement?
Ali Çehreli
acehreli at yahoo.com
Sun Apr 2 14:20:15 UTC 2023
On 4/2/23 07:07, Dom Disc wrote:
> On Sunday, 2 April 2023 at 10:33:28 UTC, Jack Applegame wrote:
>> On Friday, 31 March 2023 at 21:24:25 UTC, Steven Schveighoffer wrote:
>>> If you "forget a case statement", or refactor it, forcing you to
>>> write `default: break;` doesn't fix that.
>> `default: assert(0);` does.
>
> Yup. I like that - much better default.
> Except for assert(0) not providing any message. So if you run into this,
> it's still not obvious where to find the bug.
'final switch', which Steve included in his OP, does that. The error
message points at the 'switch' line:
void main(string[] args) {
final switch (args.length) { // Line 1106
case 1:
break;
}
}
produces
$ ./deneme my-arg
core.exception.SwitchError at deneme.d(1106): No appropriate switch clause
found
[...]
Although it's good enough, it could include the value that did not match
a clause like "... for '2'." However, it's always tricky to print just
any value for various reasons:
- A string expression may be too long to be useful; so, some trimming
may have to be performed, which may trim the distinguishing parts of a
string.
- Sensitive data may be dumped to the user.
Ali
More information about the Digitalmars-d
mailing list