C++ pattern matching is coming
ryuukk_
ryuukk.dev at gmail.com
Mon Oct 24 16:46:40 UTC 2022
On Monday, 24 October 2022 at 14:39:26 UTC, bachmeier wrote:
> 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.
What about:
```D
auto result = switch (x)
{
0,1,2 => aaa();
3 => bbb();
4 => ccc();
5 => {
int a = 1+1;
return a;
};
else => 0;
}
```
If the symbol after switch is not "``case``" or "``default``",
then the compiler can process it as "enhanced" switch
More information about the Digitalmars-d
mailing list