Pattern matching: Callback For Matching Type

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


On 30/05/2024 6:48 AM, monkyyy wrote:
> On Wednesday, 29 May 2024 at 18:24:19 UTC, Richard (Rikki) Andrew 
> Cattermole wrote:
>> Today I woke up and found I wanted to write up a proposal for pattern 
>> matching for D.
> 
> hmmm strange

I know right.

Last time this happened I wrote 6500 words in four days.

>> 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)
>> };
>> ```
> 
> I see no obvious way "value specialization" would work here

Indeed.

It would be good to make sure we're not painting ourselves in a corner 
for such language features. So if others who care about it can figure it 
out, that'll be great.



More information about the dip.ideas mailing list