-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Tracking issue for Option::xor #50512
Comments
There should also be an |
There would be no point in a |
…good point. Never mind, then. |
Add Option::xor method Implements the method requested in rust-lang#50512.
Implemented on #50553. |
@Milack27 You actually need to keep this issue open as a tracking issue. It hasn't been stabilised yet, so, it can only be used with If you could rename this issue to "Tracking issue for Option::xor" it would also help make that clear. |
@clarcharr Oops, sorry! Just fixed it. |
Aren't new methods supposed to be added via RFC? |
@pravic I don't know. The same concern was raised in #50553, but it seems the approval of the libs team is sufficient. Initially, I just posted this issue as a feature suggestion, expecting to get the opinions of the community members before going through the RFC process. Nevertheless, I can still write an RFC if it's needed. |
@pravic depends on what kind of method. Simple ones like this are implemented and left unstable to be used by the community, and usually that + a final comment period is enough to accept a feature. Ones that are more iffy on the design need RFCs though. |
@sfackler Quite a bit of time has passed since it was first implemented on the 18th of May; How do you feel about stabilizing this? |
Seconding @Centril's question, it's now February 2019 |
@rfcbot fcp merge |
Team member @sfackler has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Seems really niche, I'm not sure if adding another method to option is worth the externalities of that versus people writing match statements |
Fwiw, I think |
Just for the sake of curiosity,
Then, I had to follow the path and collect the letters. I could have used |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
Note that the same thing can also be expressed as |
@Amanieu |
Does this method exist in other languages? I've never seen such a method in OCaml or Haskell, both languages which make extensive use of option types and have been around much longer than Rust has. That should make us doubt whether this is useful enough to belong in the standard library. |
@lfairy Your point is reasonable. I'm not familiar with either OCaml or Haskell, but I'd like to point out some possible arguments in favor of Option::xor that could be taken into account when comparing Rust to those languages:
|
Haskell's standard library has the more general operation class Applicative f => Alternative f where
empty :: f a
(<|>) :: f a -> f a -> f a and |
Looking at the implementation, it seems that Haskell's implementation is an inclusive or rather than exclusive and takes the first argument if it's present regardless of the second. |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. The RFC will be merged soon. |
@Amanieu & @withoutboats: Since you have expressed doubts, can you confirm that you are OK with moving forward with this? |
…nSapin Stabilize Option::xor FCP done in rust-lang#50512 (comment) . Closes rust-lang#50512 .
…nSapin Stabilize Option::xor FCP done in rust-lang#50512 (comment) . Closes rust-lang#50512 .
@andre-vm I just wanted to point out that lhs
= Some(1).xor(Some(2)).xor(Some(3))
= None.xor(Some(3))
= Some(3)
rhs
= Some(1).xor(Some(2).xor(Some(3)))
= Some(1).xor(None)
= Some(1) However, boolean xor is associative. lhs
= (true != true) != true
= false != true
= true
rhs
= true != (true != true)
= true != false
= true Thus, it seems to me that |
I would also like to point out a simpler operation than impl<T> Option<T> {
fn and_not<U>(self, other: Option<U>) -> Option<T> {
match &other {
&None => self,
_ => None,
}
}
} The option_a.xor(option_b) = option_a.and_not(option_b).or(option_b.and_not(option_a)) Conversely, we can implement option_a.and_not(option_b) = option_a.map(Ok).xor(option_b.map(Err)).and(option_a) It would be nice to have |
Today I got myself in a situation in which I had two
Option<T>
values, and I wanted to return the only one that wasSome
. If both of them wereSome
, then I'd rather returnNone
. I think it would be perfect to have axor
method, very similar to theor
method:I think that would solve my problem nicely, and could be useful for a lot of people as well.
The text was updated successfully, but these errors were encountered: