digitalmars.D - std.sumtype needs implicit conversion to reach its full potential.
- FeepingCreature (43/43) Nov 16 2021 Just now, I was looking to extend our mock framework to allow
- Meta (4/48) Nov 16 2021 I have wanted this so often and so badly in any project in which
- StarCanopy (13/14) Nov 16 2021 Yes! Another variation of this:
- novice2 (3/4) Nov 18 2021 could you please, publish link to library?
- StarCanopy (4/9) Nov 18 2021 It isn't publicly available on its own as it's something I use
- Tejas (20/64) Nov 17 2021 That's a problem with every single User-Defined type tho
- JN (3/16) Nov 18 2021 I don't know if ["fasf": 3, "hweh": 5] should work, but it would
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
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
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
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
On Thursday, 18 November 2021 at 11:02:31 UTC, novice2 wrote:On Wednesday, 17 November 2021 at 03:49:41 UTC, StarCanopy wrote: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.... into my result library to ...could you please, publish link to library? if it possible, of couse.
Nov 18 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... ; _ ;
Nov 17 2021
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