-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Idea: Built-in way to go from bool to Option #2606
Comments
Obligatory link to the boolinator crate (and its silly test comments) I have no opinion on whether any of this should be uplifted to |
Personally, I think adding an external crate offsets the convenience of having it. I would rather just have this in std. |
cc #2180 |
I think we should add this, and the closure-based version. The outcome of #2180 is that we didn’t find consensus on names. |
For now, I tend to do |
This isn't quite equivalent: if the |
I find myself wanting the ability to "map" a |
Another +1 to this. Should I do a PR for it? |
@canndrew please do! |
I personally would name this |
Perhaps |
impl bool {
fn ???<T>(self, b: T) -> Option<T>; // "closure-less map"
fn map<T, F>(self, f: F) -> Option<T> where F: FnOnce() -> T;
fn and<T>(self, optb: Option<T>) -> Option<T>;
fn and_then<T, F>(self, f: F) -> Option<T> where F: FnOnce() -> Option<T>;
}
let my_opt_value = my_bool.???(get_value());
let my_opt_value = my_bool.map(|| get_value());
let my_opt_value = my_bool.and(get_opt_value());
let my_opt_value = my_bool.and_then(|| get_opt_value()); |
Perhaps my_opt.map(|_| ()) // Ignore the data, but keep the "status"
my_opt.map(|_| { // Same as `if my_opt.is_some() { ... }`
do_things_with_side_effects();
return_something()
}) Or perhaps it's not needed and |
@petrochenkov the issue with using In Haskell terms: For Option it would look like this: Personally I'd rather go with either @mark-i-m's suggested |
Any update on the situation? This would be really great to see available in |
I've opened an RFC here: #2757. |
The idea is pretty simple: Add a function called
some
to booleans such that you can automatically convert to an option.The basic implementation wouldn't be much more than
The purpose of this would be to be able to write something like
regex.is_match(string).some(token)
without having to explicitly set up an if-statement.Maybe also add in an
*_or_else
style function that takes a closure as an input, such that the true case is only ever allocated if it succeeds:regex.is_match(string).and_some(|| build_token())
.Yes/No/Maybe?
I'd like to hear your opinions :)
The text was updated successfully, but these errors were encountered: