-
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
core: impl From<T> for Option<T> #34828
Conversation
Also see rust-lang/rfcs#1402 and #33170 |
I know it's been discussed a bunch before. Having the impl exist by itself doesn't mean the APIs need to use it. |
Indeed, thanks for the cc @jonas-schievink! @seanmonstar this has been discussed and turned down before, do you have new information which has changed since the decision was last made? If not we can probably just close this. |
@alexcrichton a difference to the previous PR is that this only adds the The choice to alter APIs to use that is not made in this PR, and so the standard library does not need to worry about stabilizing "optional arguments" in itself that could conflict with lang features. |
Here's a longer rationale: I don't see these as "optional arguments" in the usual sense. I have several arguments that are definitely not optional, they must have an answer, but whether that answer has a value, or a Optional arguments look like this: fn action(&mut self, action: Action, dur: Option<Duration> = None) {
}
thing.action(Action::Read); // same as thing.action(Action::Read, None) Instead, I have methods that do not make sense to have this form of "optional arguments": fn timeout(&mut self, dur: Option<Duration>) {
}
thing.timeout(); // this would not make sense
thing.timeout(None); // you must give an answer
thing.timeout(Some(10)); The point with this method is to specify if there should be a timeout, and if so, for how long. It does not make sense for this to have a signature of So, what to do. Seems there's 4 options.
|
The fact that we have 2 issues and 2 PRs for exactly the same thing (and without any of the optional arguments stuff, although I see it being as a primary motivation), proves that it is a very desired feature, even without some implementation of optional arguments. I know I wanted it for something that’s not optional arguments in some generic code. |
Turned down without any motivation except for vague concerns about possible future language features. |
From my recollection, the concerns were over modifications to the timeout APIs in particular. The |
I think the concern here is that library authors will start using this feature for optional arguments. Then, if rust ever adds built-in optional/default argument support, there'll be two different ways to do it. (Note: I'm still in favor of this feature). |
Yes it sounds like this is different than #33170 in that it's not proposing the standard library uses it for optional arguments, but the motivation is still optional arguments in external libraries. Adding the |
We discussed this during libs triage today and the decision was to merge given that this PR is only adding a |
@bors: r+ Thanks again for the PR @seanmonstar! |
📌 Commit 501c615 has been approved by |
⌛ Testing commit 501c615 with merge 18e4090... |
💔 Test failed - auto-linux-64-opt |
@bors: retry On Tue, Jul 19, 2016 at 12:50 PM, bors notifications@github.com wrote:
|
⌛ Testing commit 501c615 with merge db889f0... |
💔 Test failed - auto-mac-64-opt-rustbuild |
@bors: retry On Tue, Jul 19, 2016 at 9:23 PM, bors notifications@github.com wrote:
|
@bors: r- Looks like travis failure is legitimate |
Added |
⌛ Testing commit fbfee42 with merge 13700cf... |
💔 Test failed - auto-linux-64-cargotest |
@bors: retry On Wed, Jul 20, 2016 at 6:04 PM, bors notifications@github.com wrote:
|
core: impl From<T> for Option<T> First, the semantics of this `impl` seem spot on. If I have a value `T`, and I wish to make a `Option<T>`, then `Option::from(val)` should always give `Some(val)`. Second, this allows improvement for several APIs that currently take `Option<T>` as arguments. Consider: ```rust fn set_read_timeout(&mut self, timeout: Option<u32>) { // ... } x.set_read_timeout(Some(30)); x.set_read_timeout(Some(10)); x.set_read_timeout(None); ``` With this `impl`: ```rust fn set_read_timeout<T: Into<Option<u32>>>(&mut self, timeout: T) { let timeout = timeout.into(); // ... } x.set_read_timeout(30); x.set_read_timeout(10); x.set_read_timeout(Some(10)); // backwards compatible x.set_read_timeout(None); ``` The change to those methods aren't included, but could be modified later. r? @sfackler
First, the semantics of this
impl
seem spot on. If I have a valueT
, and I wish to make aOption<T>
, thenOption::from(val)
should always giveSome(val)
.Second, this allows improvement for several APIs that currently take
Option<T>
as arguments. Consider:With this
impl
:The change to those methods aren't included, but could be modified later.
r? @sfackler