-
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
Modernizing parts of Option, Result, and Default #9115
Conversation
It seems the docs for |
|
||
use std::vec; | ||
|
||
pub fn expand_deriving_default(cx: @ExtCtxt, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a mention of this new one to the "deriving" section of the manual?
Vecs are not numeric types, so it doesn't make sense for them to implement Zero.
Options are not numeric types, so it doesn't make sense for them to implement Zero.
|
||
/// Returns self or `Some`-wrapped default value | ||
#[inline] | ||
pub fn or_default(self) -> Option<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this x.or_default()
different to Some(x.unwrap_or_default())
? (Is it worth having a function just for this?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look at that. I added it because we already had .or_zero
. I'll remove .or_default
and .or_zero
in a separate PR since this one is getting pretty weighty already.
This looks fine to me from a code point-of-view, but it feels like a significant addition of code that should be also approved by someone who's not me. Also, cc #9157. |
This is a series of patches to modernize option and result. The highlights are: * rename `.unwrap_or_default(value)` and etc to `.unwrap_or(value)` * add `.unwrap_or_default()` that uses the `Default` trait * add `Default` implementations for vecs, HashMap, Option * add `Option.and(T) -> Option<T>`, `Option.and_then(&fn() -> Option<T>) -> Option<T>`, `Option.or(T) -> Option<T>`, and `Option.or_else(&fn() -> Option<T>) -> Option<T>` * add `option::ToOption`, `option::IntoOption`, `option::AsOption`, `result::ToResult`, `result::IntoResult`, `result::AsResult`, `either::ToEither`, and `either::IntoEither`, `either::AsEither` * renamed `Option::chain*` and `Result::chain*` to `and_then` and `or_else` to avoid the eventual collision with `Iterator.chain`. * Added a bunch of impls of `Default` * Added a `#[deriving(Default)]` syntax extension * Removed impls of `Zero` for `Option<T>` and vecs.
…logiq `new_without_default`: ignore const generics/lifetime params on `fn new` Fixes rust-lang#9113 No longer lints if `fn new` has any params changelog: [`new_without_default`]: no longer lints const generics and lifetime params on `fn new`
This is a series of patches to modernize option and result. The highlights are:
.unwrap_or_default(value)
and etc to.unwrap_or(value)
.unwrap_or_default()
that uses theDefault
traitDefault
implementations for vecs, HashMap, OptionOption.and(T) -> Option<T>
,Option.and_then(&fn() -> Option<T>) -> Option<T>
,Option.or(T) -> Option<T>
, andOption.or_else(&fn() -> Option<T>) -> Option<T>
option::ToOption
,option::IntoOption
,option::AsOption
,result::ToResult
,result::IntoResult
,result::AsResult
,either::ToEither
, andeither::IntoEither
,either::AsEither
Option::chain*
andResult::chain*
toand_then
andor_else
to avoid the eventual collision withIterator.chain
.Default
#[deriving(Default)]
syntax extensionZero
forOption<T>
and vecs.