www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.sumtype needs implicit conversion to reach its full potential.

reply FeepingCreature <feepingcreature gmail.com> writes:
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.
Nov 16 2021
next sibling parent Meta <jared771 gmail.com> writes:
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!
Nov 16 2021
prev sibling next sibling parent reply StarCanopy <starcanopy protonmail.com> writes:
On Tuesday, 16 November 2021 at 11:15:27 UTC, FeepingCreature 
wrote:
 [...]
Yes! Another variation of this: ```d SumType!(string, Err) doSomething() { // The dream: return Err("Something happened"); return typeof(return)(Err("Something happened")); } ``` I had to introduce a string mixin and some help functions into my result library to help alleviate this noise. Perhaps the introduction of an `opImplicitConv` or something akin to this could help?
Nov 16 2021
parent reply novice2 <sorryno em.ail> writes:
On Wednesday, 17 November 2021 at 03:49:41 UTC, StarCanopy wrote:
 ... into my result library to ...
could you please, publish link to library? if it possible, of couse.
Nov 18 2021
parent StarCanopy <starcanopy protonmail.com> writes:
On Thursday, 18 November 2021 at 11:02:31 UTC, novice2 wrote:
 On Wednesday, 17 November 2021 at 03:49:41 UTC, StarCanopy 
 wrote:
 ... into my result library to ...
could you please, publish link to library? if it possible, of couse.
It isn't publicly available on its own as it's something I use internally within my own projects. However, you might be interested in this, https://code.dlang.org/packages/expected.
Nov 18 2021
prev sibling parent reply Tejas <notrealemail gmail.com> writes:
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... ; _ ;
Nov 17 2021
parent JN <666total wp.pl> writes:
On Wednesday, 17 November 2021 at 08:34:28 UTC, Tejas wrote:
 ```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
 }
 ```
I don't know if ["fasf": 3, "hweh": 5] should work, but it would be nice if ["fasf": {a: 3}, "hweh": {a: 5}] worked.
Nov 18 2021