-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overloaded Add
on Option has arguably the wrong behavior
#6002
Comments
+1 |
Is there any precedence of this behavior from other programming languages (e.g., Haskell, Scala, Standard ML)? I'm not very familiar with type theory, so my novice eyes see the original behavior as correct; ultimately, I would err on the side of whatever has proven best in other languages (especially as that seems to be Rust's philosophy). |
AFAIK other languages don't define + for their Option types. They have you use It's not very generic right now because it assumes that copying is the behaviour you want when one of the two is Defining Does it really make sense to pass |
I think that by analogy with the Maybe monad in Haskell, it would make sense for I'm neutral about whether we want to have an instance for |
@thestinger you're welcome to remove it if you think it's a poor idea. My primary concern is that increasing the friction required to make use of Options will simply result in them not being used at all. And since users can't implement this themselves, this is a decision that we have to make now. |
@catamorphism: Haskell's typeclass |
@kud1ing I guess I was thinking of the |
I think we have to decide, whether I think we need to support both representations:
Whatever we come up with, we should explain the decision at least in the code. |
I don't think there's anything to decide, "no value" and "error" are the same thing if there's only one error that could happen and it means a value couldn't be returned. If there's more than one possible error and there's a meaningful distinction between them that the caller would care about, it should use An |
@kud1ing |
Ask two persons how many pair of shoes they own. One does not reply, the other says 10. |
I get where you're going with this, @kud1ing , but I don't think this question has a single right answer. The team just needs to agree on something and document it. |
@catamorphism: That's why i think we have to be aware of what we want to express and write that down. First so that users know what behaviour to expect and second so we don't change our implementation again and again. |
Two questions: Which behaviour is most common? Which is most annoying to implement in "userspace"? |
I think
|
Haskell can have 4 different implementations for sums: http://byorgey.wordpress.com/2011/04/18/monoids-for-maybe/
I think
and thus suggest we
|
Better yet: reconsider the places that use either option or result this way, and if it is a rare error, add / rewrite to a version that uses a condition. |
I would strongly vote for
The latter works generically for any function. It's how Haskell defines the Haskell doesn't overload the numerical operators like this by default because the fully generic way to do it would be:
and that would overlap with every other
I think having |
Visiting for triage, have there been any more thoughts on this? Also nominating for "backwards compatible". An option I haven't seen is just not having a + operation defined on options. Has this been ruled out? |
@toddaaro I originally overloaded |
I fully agree with @glehel here: You can always map If we compare it with @kud1ing 's list the use cases could be fulfilled like this::
|
My vote: do not define |
accepted for backwards-compatible milestone |
I agree with @nikomatsakis at this point. |
Closes #6002 There is consensus that the current implementation should be changed or removed, so removing it seems like the right decision for now.
This is an alternative version to rust-lang#8268, where instead of transitioning to `get()` completely, I transitioned to `unwrap()` completely. My reasoning for also opening this PR is that having two different functions with identical behavior on a common datatype is bad for consistency and confusing for users, and should be solved as soon as possible. The fact that apparently half the code uses `get()`, and the other half `unwrap()` only makes it worse. If the final naming decision ends up different, there needs to be a big renaming anyway, but until then it should at least be consistent. --- - Made naming schemes consistent between Option, Result and Either - Lifted the quality of the either and result module to that of option - Changed Options Add implementation to work like the maybe Monad (return None if any of the inputs is None) See rust-lang#6002, especially my last comment. - Removed duplicate Option::get and renamed all related functions to use the term `unwrap` instead See also rust-lang#7887. Todo: Adding testcases for all function in the three modules. Even without the few functions I added, the coverage wasn't complete to begin with. But I'd rather do that as a follow up PR, I've touched to much code here already, need to go through them again later.
A while ago I overloaded
Add
on Option types such thatSome(1) + None == Some(1)
. I've since had a change of heart and believe that instead it should beSome(1) + None == None
. An informal poll in IRC seems to agree.The text was updated successfully, but these errors were encountered: