Add opUnwrapIfTrue operator overload for structs#22570
Add opUnwrapIfTrue operator overload for structs#22570rikkimax wants to merge 1 commit intodlang:masterfrom
Conversation
|
Thanks for your pull request and interest in making D better, @rikkimax! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + dmd#22570" |
|
Closes: #20300 I will note that this issue has appeared in PhobosV2, with Nullable and its get method being available via alias this. |
71514ed to
4edcf62
Compare
|
I still don't see what this has to do with |
Well, I assume that it's because the thought was that this would be for a type that's returned from a function to give a result or indicate a failure (e.g. a type which holds a value or an error code / message, or something like a That being said, I don't see anything about the proposed feature which requires At its core, this seems to just be proposing that if a type is explicitly convertible to For a type that is explicitly intended to be returned as the result of a function (as opposed to something like For a So, my gut reaction is that this proposed feature really doesn't make sense for a proper result type, because a proper result type shouldn't just be a value or not. It should have a value or information on the error that occurred. And for a I can see why someone might want this, but I'm not sure that it ultimately makes sense. I'd argue that overloading |
This requires The problem with splitting out the getting of value is you can do it without the check. This guarantees that it is paired together. |
2be2f18 to
9ae65ec
Compare
|
I've overhauled the implementation, making it simpler. I've included in changelog explicit mentions that mustuse is not required. |
9ae65ec to
d6d434f
Compare
086ea80 to
29d400e
Compare
|
I am applying feedback to PR, specifically making it much more explicit that an unwrap is occuring with a function call. To trigger it, it's via an alias, to the operator overload, it is not required. |
29d400e to
5446582
Compare
5446582 to
6fc0ee9
Compare
| if (int i = oi.unwrap) { | ||
| // got a value! | ||
| assert(i == 2); | ||
| } else { | ||
| assert(0); // error, there is meant to be a value | ||
| } |
There was a problem hiding this comment.
Can't this be done with monadic operators, to take a leaf from C++ if we're going with Optional as the reference example.
oi.and_then((i) { assert(i == 2); return Optional!int(i); })
.or_else(() { assert(0); });Or something like that.
There was a problem hiding this comment.
You lose control flow, no more returning which is a common thing to want to do in the else, and even the true.
It also introduces a lot of REALLY FUN THINGS, like closure context's which right now don't do cleanup.
There was a problem hiding this comment.
No idea what you are referring to. No heap closure is involved.
struct Optional(Type) {
...
// Omitting T function(...) overloads for brevity
auto and_then(T)(scope T delegate(Type) fun)
=> (haveValue) ? Optional!T(fun(value)) : Optional!T();
auto or_else(scope Type delegate() fun)
=> (haveValue) ? this : Optional!Type(fun());
auto transform(T)(scope T delegate(Type) fun)
=> (haveValue) ? Optional!T(fun(value)) : Optional!T();
Type value_or(Type fallback)
=> (haveValue) ? value : fallback;
}string test(Optional!string v)
{
import std.conv;
return v
.or_else(() => "0")
.and_then((s) => to!int(s))
.transform((n) => n + 1)
.transform((n) => to!string(n))
.value_or("NaN");
}
Optional!string("43").test.writeln; // "44"
This is an improvement on
@mustuseto allow only getting access to a value if it has been checked to be true.Does not use DFA to do this, instead if statement + a var declaration in its condition.
No DIP nor has it been approved at a meeting.
It is the result of my report on an alternative exception mechanism.
I was able to take an existing approach that I've been mulling over that relied on UDA's and a DFA for path sensitivity and turn it into a simple operator overload hook instead.