C++ pattern matching is coming

Walter Bright newshound2 at digitalmars.com
Sun Oct 23 23:40:21 UTC 2022


On 10/23/2022 4:21 PM, Timon Gehr wrote:
> Actually, each case does introduce a new scope:

Ack, you're right. Going back and forth between C and D confuses me.


> Another problem is that switch cases are not ordered 
> and are supposed to be disjoint. But for pattern matching, the semantics is 
> usually to try each pattern in order, where patterns can overlap and you are 
> supposed to put more specific patterns earlier.

Yup.


> Of course, one could think about additionally adding some pattern support to 
> switch, where the compiler checks that all patterns are disjoint, but does not 
> seem very nice to use. Also seems like somewhat of a hassle to implement.

The syntax of switch is old-fashioned, too. A more modern one would be:

     match (x)
     {
	0 => foo();
         3 => bar();
     }

Note the lack of need for `break`.


More information about the Digitalmars-d mailing list