Pattern matching: Callback For Matching Type

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


Today I woke up and found I wanted to write up a proposal for 
pattern matching for D.

I had been putting this one off as it wasn't really of interest 
to me and wanted to see what Walter had come up with for last 
DConf Online (it was changed at last minute).

As a feature it seems fairly straightforward; we as a community 
want a context-aware keyword called ``match`` that operates on a 
type or a tuple and performs matching with support for multiple 
dispatch and inference of pattern types. Without full pattern 
matching of literals or nested types.

This will work for sum types without the need to add them to the 
language beforehand.

Permanent: 
https://gist.github.com/rikkimax/79cbe199618b3f99104f7df2fc2a9681/e0e6dd44f7477be962e6c71312ba08a1fe1ee8d6
Latest: 
https://gist.github.com/rikkimax/79cbe199618b3f99104f7df2fc2a9681

```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)
};
```


More information about the dip.ideas mailing list