-
Notifications
You must be signed in to change notification settings - Fork 11.3k
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
[stdlib] Adds macros for option #18421
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
3 Ignored Deployments
|
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.
Looks amazing! Thank you so much!
Just some style nits.
Also I would ask that we make sure @tzakian gives this a once over before we land, just since that he helped come up with a lot of the names for the macros here
if (o.is_some()) { | ||
$f(o.destroy_some()); | ||
} |
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.
style nit
if (o.is_some()) { | |
$f(o.destroy_some()); | |
} | |
if (o.is_some()) $f(o.destroy_some()) |
if (o.is_some()) { | ||
$f(o.borrow()); | ||
} |
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.
if (o.is_some()) { | |
$f(o.borrow()); | |
} | |
if (o.is_some()) $f(o.borrow()) |
/// Execute a closure on the mutable reference to the value inside `t` if it holds one. | ||
public macro fun do_mut<$T>($o: &mut Option<$T>, $f: |&mut $T|) { | ||
let o = $o; | ||
if (o.is_some()) $f(o.borrow_mut()); |
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.
if (o.is_some()) $f(o.borrow_mut()); | |
if (o.is_some()) $f(o.borrow_mut()) |
if (o.is_some()) o | ||
else $default |
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.
🔥
/// Return `None` if the value is `None`, otherwise return `Option<T>` if the predicate `f` returns true. | ||
public macro fun filter<$T: drop>($o: Option<$T>, $f: |&$T| -> bool): Option<$T> { | ||
let o = $o; | ||
if (o.is_some() && $f(o.borrow())) o |
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.
Beautiful, love how this one turned out
option::some(5).destroy!(|x| counter = x); | ||
option::some(10).do!(|x| counter = counter + x); | ||
|
||
assert!(counter == 15); |
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.
I snuck in std::unit_test::assert_eq
a while back if you want to give it a shot. This is fine though
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.
Looks amazing! 🚀
Description
Adds macros for option, currently, contains
do
anddestroy
(identical).Test plan
Features tests.
Release notes
Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required.
For each box you select, include information after the relevant heading that describes the impact of your changes that a user might notice and any actions they must take to implement updates.