Should D have control-flow expressions? IS it possible?

DanielG simpletangent at gmail.com
Sat Sep 12 05:27:01 UTC 2026


(These are comments on your feature as a whole - let me know if 
there's a better thread for it. I don't see one in the DIP Ideas 
/ DIP Devel forums)

1. THIS is what sumtypes should look like!! I really hope you 
will create a competing DIP for this - I do NOT want 
type-unions-masquerading-as-sumtypes to be what we end up with.

2. IMO, drop the bare type cases. [It's 
cleaner.](https://i.imgur.com/nZ5q20o.jpeg) They don't really add 
anything, and bare type matching in `switch` should be the 
responsibility of whoever adds type unions to the language 
(versus vanilla sumtypes). Sumtypes and type unions should be 
orthogonal features, not mixed together.

3. C# simply has two forms of the `switch` statement: the 
original statement-based switch, and the newer expression-based 
switch (`value switch { ... }` instead of `switch(value) { ... 
}`). Does your feature already work in a normal switch? If so, I 
don't think the extra complexity (of control-flow expressions) is 
justified.

What I'm saying is, if somebody needs a break/continue, they can 
just use the normal statement-style switch. This would feel 
perfectly natural in D:

```d
	    // resort to statement-style switch when you need 
break/continue
         size_t bytesHandled;
         switch (pkt)
         {
             case Data(slice):
                 bytesHandled = handlePayload(slice);
                 break;
             case Ping(ts):
                 bytesHandled = sendPong(ts);
                 break;
             case Heartbeat():
                 continue;
             case EndOfStream():
                 break;
             case Reset(err):
                 assert(0, "Reset");
         };
```



More information about the Digitalmars-d mailing list