removing default case requirement?
Salih Dincer
salihdb at hotmail.com
Sat Apr 1 10:08:02 UTC 2023
On Saturday, 1 April 2023 at 07:32:28 UTC, claptrap wrote:
> How often does anyone use "default: break"?
If you are a lazy (actually talented) programmer 😀
In fact, if you use enums, the compiler will warn you with the
comment in the code.
But this way program produces 2 lines of output likes this:
> humid: 2
> I'm in Silifke Asthma Cave
I learned this method from Walter in Dconf'22. Remove the
toggle-comment at the beginning of the final to try.
```d
enum Weather { hot, humid, cold,
sunny, windy, cloudy,
drizzling, rainy, foggy
}
string shallWeGo(Weather w)
{
//final switch(w) {/*
switch(w) {
default: return home();//*/
case Weather.hot:
w.writeln(": ", 1);
return sea();
case Weather.humid:
w.writeln(": ", 2);
return cave();
case Weather.sunny:
w.writeln(": ", 3);
return beach();
case Weather.cloudy:
w.writeln(": ", 3);
return home;
}
}
import std.stdio;
void main()
{
auto choice = Weather.humid;
choice.shallWeGo().writeln;
}
auto sea() => "";
auto home() => "";
auto cave()
=> "I'm in Silifke Asthma Cave";
auto beach() => "";
/* Without the final keyword you will get a compilation error:
onlineapp.d(8): Error: missing cases for `enum` members in `final
switch`:
onlineapp.d(8): `cold`
onlineapp.d(8): `windy`
onlineapp.d(8): `drizzling`
onlineapp.d(8): `rainy`
onlineapp.d(8): `foggy`
*/
```
SDB at 79
More information about the Digitalmars-d
mailing list