Pattern matching: Callback For Matching Type

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Wed May 29 18:41:22 UTC 2024


On 30/05/2024 6:33 AM, Daniel N wrote:
> On Wednesday, 29 May 2024 at 18:24:19 UTC, Richard (Rikki) Andrew 
> Cattermole wrote:
>> ```d
>> alias MTU = MyTaggedUnion!(int, float, string);
>>
>> MTU mtu = MTU(1.5);
>>
>> mtu.match {
>>     (float v) => writeln("a float! ", v),
>>     v => writeln("catch all! ", v)
>> };
>> ```
> 
> why not
> (auto v)
> for syntax consistency?

It already is consistent for syntax.

```d
// Use a catch-all handler to give a default result.
bool isFahrenheit(Temperature t)
{
     return t.match!(
         (Fahrenheit f) => true,
         _ => false
     );
}
```

https://dlang.org/phobos/std_sumtype.html


More information about the dip.ideas mailing list