C++ pattern matching is coming

bachmeier no at spam.net
Mon Oct 24 14:39:26 UTC 2022


On Monday, 24 October 2022 at 14:05:47 UTC, Quirin Schroll wrote:

>> A more modern one would be:
>>
>>     match (x)
>>     {
>>         0 => foo();
>>         3 => bar();
>>     }
>>
>> Note the lack of need for `break`.
>
> No. Java re-used the `swtich` keyword and IMO is correct in 
> doing so. The rest is great as in Java.

IMO `match` is a much better name than `switch`, which I didn't 
find intuitive when I first encountered it, and it only made 
sense because it was followed by a series of `case` statements. I 
would like the above syntax plus

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

where `foo` and `bar` are functions taking one argument, and

```
match (x)
{
       case(0):
          lines of code
          break;
       3 => &foo;
       4 => bar();
}
```

No idea if this would be possible/sensible, but it's one of the 
few things that would be a big improvement when I'm writing D 
programs.


More information about the Digitalmars-d mailing list