std.sumtype?
Paul Backus
snarwin at gmail.com
Tue Mar 30 16:32:21 UTC 2021
On Tuesday, 30 March 2021 at 03:13:04 UTC, Jon Degenhardt wrote:
> 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.
I wouldn't expect users to jump straight to the fully-generalized
version, but you don't need a lot of fancy template machinery to
get the same result in a specific case. For example:
(val) {
alias T = typeof(val);
static assert(is(T == Foo0) || is(T == Foo1) || is(T ==
Foo2)
|| is(T == Foo3) || is(T == Foo4));
doSomething1;
}
And once you have that, it's not a huge leap to notice that you
can clean it up a bit with `staticIndexOf`. And once you do that
a few times, maybe you start to think about how to generalize it.
I will grant that it may not be obvious that you can use `static
assert` with `match` in this way to begin with. That's something
that can be addressed by improving SumType's documentation.
> 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?
The thought is: I don't know in advance what will be common and
what won't, so I think it's best to wait for feedback before
adding facilities like this. In the long run, I expect we will
see some utility templates like `restrictTo` added--either to the
`sumtype` module, or to a more general module like
`std.functional` if they stand on their own.
> 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.
Your guess that the underscore is not special is correct. This
should be clear from reading the documentation for match [1], but
I expect there are many users who read only the examples and skip
the prose, so it would be best to add a note about this to the
examples as well.
[1] https://pbackus.github.io/sumtype/sumtype.match.html
More information about the Digitalmars-d
mailing list