removing default case requirement?

Paul Backus snarwin at gmail.com
Sat Apr 1 04:51:18 UTC 2023


On Saturday, 1 April 2023 at 03:25:15 UTC, Salih Dincer wrote:
> IMO, we should discuss "should the `break` keyword be removed"

I believe the reason D requires you to write `break` rather than 
making it automatic/implicit is to avoid unexpected behavioral 
changes when porting C code to D. For example, if you have C code 
like this:


```c
switch (x) {
     case 1:
         do_stuff();
         /* fallthrough */
     case 2:
         do_other_stuff();
         break;
}
```

...and you try to compile it as D code, you will get a compile 
error, because D requires either `break`, `return`, or `goto 
case` at the end of each `case` block.

If the D compiler inserted an implicit `break;` at the end of the 
first `case` block automatically, then instead of an error, you 
would get different behavior at runtime. This would make porting 
C projects to D more error prone.


More information about the Digitalmars-d mailing list