removing default case requirement?
Timon Gehr
timon.gehr at gmx.ch
Sat Apr 1 00:31:57 UTC 2023
On 4/1/23 01:52, Steven Schveighoffer wrote:
>
> The open-endededness is not that you forgot a case, it's that you didn't
> care about that case, or it's not something that requires a case in that
> particular switch.
> ...
But which one is it? I'd like to know when I read the code.
> I really am puzzled by the idea that the compiler requiring one to put
> `default:` is helping them remember things.
> ...
The point of `default: break;` is that it explicitly states "I am doing
nothing if it's not one of the other cases". A switch can be pretty
long, so it's convenient to be able to search for the default case and
be confident you won't end up in an unrelated long switch.
> To be honest, I'm OK with the requirement. I really am, I can write
> `default: break;` and I actually do with regularity without forgetting!
> But it's a hard one to explain to newcomers. It's just one of those
> things that I explain with "D makes you do this, get over it". I can't
> really explain that it's helpful with a straight face.
There are multiple common ways to deal with the default:
- You actually meant to use `final switch`.
- Do some explicit action
- assert(0);
- enforce(0,"TODO: handle case")
- Do nothing
I seldomly want to do nothing. Why should that be the implicit default?
More information about the Digitalmars-d
mailing list