Pattern matching: Callback For Matching Type
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Wed May 29 19:44:32 UTC 2024
On 30/05/2024 7:38 AM, Harry Gillanders 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.
>>
>> Permanent:
>> https://gist.github.com/rikkimax/79cbe199618b3f99104f7df2fc2a9681/e0e6dd44f7477be962e6c71312ba08a1fe1ee8d6
>> Latest: https://gist.github.com/rikkimax/79cbe199618b3f99104f7df2fc2a9681
>
>
>> A __tag member must evaluate to an integer
>
> Does this mean `__tag` can also be a member-function? For types where
> the tag doesn't need any storage (like NaN-boxing, disjoint enums, etc.).
Yes. You would want this to protect your tag value so that it would be
read only.
> I think it would be nice if the match-patterns were semicolon delimited,
> so that `static if` and `static foreach` and whatnot could be used in a
> `match` block—something like:
>
> ```d
> mtu1.match {
> static if (condition) {
> (float v) => writeln("a float! ", v);
> }
>
> static foreach (T; Types) {
> (T v) => writeln(T.stringof, " ", v);
> }
>
> v => writeln("catch all! ", v);
> }
> ```
That could be a good reason to switch.
Although it can be done using a fallback ``(arg) { ... },`` instead.
More information about the dip.ideas
mailing list