std.sumtype?

Jon Degenhardt jond at noreply.com
Tue Mar 30 03:13:04 UTC 2021


On Thursday, 25 March 2021 at 15:17:31 UTC, Paul Backus wrote:
> Only if you don't know what you're doing. Check this out:
>
>     template restrictTo(Args...)
>         if (Args.length >= 1)
>     {
>         import std.meta : IndexOf = staticIndexOf;
>
>         alias Types = Args[0 .. $-1];
>         alias fun = Args[$];
>
>         auto ref restrictTo(T)(auto ref T value)
>             if (IndexOf!(T, Types) >= 0);
>         {
>             import core.lifetime: forward
>
>             static assert(IndexOf!(T, Types) >= 0);
>             return fun(forward!value);
>         }
>     }
>
> Usage:
>
>     someType.match!(
>         restrictTo!(Foo0, Foo1, Foo2, Foo3, Foo4,
>             val => doSomething1
>         ),
>         restrictTo!(Bar, Baz, FooBar,
>             val => doSomething2
>         ),
>         _ => doDefaultThings
>     );

This comment is not intended as criticism. SumType looks like a 
very good package. But, I am confused by this response.

In particular, the expectation for when such a technique would be 
used. Use of D's case-range statement is not a particularly 
advanced technique. But writing the 'restrictTo' template 
requires a good bit more knowledge. I doubt the intent is that 
people will write the equivalent of 'restrictTo' whenever coming 
across a 'match' use like this. But I don't see 'restrictTo' 
included with SumType. Is the thought that this will be rare that 
it won't be needed? Or perhaps, that with additional experience, 
such a facility might be added to SumType later? Or something 
much more basic that I'm missing?

Separate thing - I think the catch-all handler could be a bit 
better documented. Is the lack of a type that triggers it? Or is 
underscore ('_') special (ala Scala). If the latter, then are the 
'_1' and '_2' forms shown in the multiple dispatch examples 
special? (I'm guessing that there's nothing special about the 
underscore forms, but people familiar with Scala might assume 
some else.) I'm looking at the docs here: 
https://pbackus.github.io/sumtype/sumtype.SumType.html.

--Jon



More information about the Digitalmars-d mailing list