std.sumtype needs implicit conversion to reach its full potential.
Meta
jared771 at gmail.com
Tue Nov 16 13:34:45 UTC 2021
On Tuesday, 16 November 2021 at 11:15:27 UTC, FeepingCreature
wrote:
> Just now, I was looking to extend our mock framework to allow
> leaving single parameters unspecified. "Okay," I thought, "this
> shouldn't be too hard":
>
> ```
> import std.sumtype;
>
> struct Mocker
> {
> static struct any {}
> }
>
> alias typeOrAny(T) = sumtype!(T, Mocker.any);
>
> template expectationMethod(alias fn)
> {
> alias ExpectedParams = staticMap!(typeOrAny, Parameters!fn);
>
> void method(ExpectedParams params)
> }
>
> .....
>
> class TestClass {
> void call(string name, int value);
> }
>
> unittest
> {
> auto mockedClass = mock!TestClass;
>
> mockedClass.expect.call("Hello World", Mocker.any); // So
> terse and elegant!
> }
> ```
>
> But, actually, that doesn't work: here's what you currently
> need to write in D.
>
> ```
> unittest
> {
> ...
> mockedClass.expect.call(typeOrAny!string("Hello World"),
> typeOrAny!int(Mocker.any));
> ```
>
> I don't care if it needs a feature with two or even three
> underscores in front. I don't care if it's undocumented. I
> don't care if it only works for `std.sumtype`, even though
> `std.typecons: nullable` is *begging* for the same
> functionality; or if you literally write into the compiler
> license that you can sue people for thousands of dollars if
> they use it badly.
>
> Just, please, give std.sumtype the ability to be implicitly
> constructed from a member type in function parameters.
I have wanted this so often and so badly in any project in which
I've used sumtype!
More information about the Digitalmars-d
mailing list