Thread overview
std.sumtype needs implicit conversion to reach its full potential.
Nov 16, 2021
FeepingCreature
Nov 16, 2021
Meta
Nov 17, 2021
StarCanopy
Nov 18, 2021
novice2
Nov 19, 2021
StarCanopy
Nov 17, 2021
Tejas
Nov 18, 2021
JN
November 16, 2021

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.

November 16, 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!

November 17, 2021

On Tuesday, 16 November 2021 at 11:15:27 UTC, FeepingCreature wrote:

>

[...]

Yes! Another variation of this:

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?

November 17, 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

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... ; _ ;

November 18, 2021

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.

November 18, 2021

On Wednesday, 17 November 2021 at 08:34:28 UTC, Tejas wrote:

>
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.

November 19, 2021

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.