logoalt Hacker News

epagelast Saturday at 5:58 PM3 repliesview on HN

I think the switch statement design is a foot gun: defaults to fall-through when empty and break when there is a body.

https://c3-lang.org/language-overview/examples/#enum-and-swi...


Replies

gkbrklast Saturday at 6:24 PM

This feels very natural though, in a "principle of least surprise" kinda way. This is what you'd expect a properly designed switch statement to do.

show 3 replies
riazrizvilast Saturday at 7:06 PM

If I aimed and shot a gun at my foot and a bullet didn’t go through it, I would trash the gun.

show 2 replies
bluecalmlast Saturday at 8:18 PM

I agree it's not the best choice. I mean it's true that you almost always want fall-through when the body is empty and break where it isn't but maybe it would be better to at least require explicit break (or fall-through keyword) and just make it a compiler error if one is missing and the body is not empty. That would be the least surprising design imo.