std.sumtype needs implicit conversion to reach its full potential.

Tejas notrealemail at gmail.com
Wed Nov 17 08:34:28 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.

That's a problem with every single User-Defined type tho

```d
import std;
struct A_VERY_LARGE_NAME{
     int a;
}
void main()
{
     //A_VERY_LARGE_NAME[string] AA = ["fasf": 3, "hweh":5]; 
		//doesn't work :(
     A_VERY_LARGE_NAME[string] AA = ["fasf": A_VERY_LARGE_NAME(3), 
"hweh":A_VERY_LARGE_NAME(5)]; 	//works
}
```

It wouldn't even be that bad to write if autocomplete was decent, 
but that code while reading... >_<


And D just hates implicit conversions(`alias this` considered bad 
and so on...) so...  ; _ ;


More information about the Digitalmars-d mailing list