-
Notifications
You must be signed in to change notification settings - Fork 550
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
feat(corelib): OptionTrait::get_or_insert_with #7082
Conversation
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.
Reviewed all commit messages.
Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @hudem1)
corelib/src/option.cairo
line 812 at r1 (raw file):
}; self.unwrap()
Suggestion:
match self {
Option::Some(value) => value,
Option::None => {
let value = f();
self = Option::Some(value);
value
}
}
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'm not sure we should have this, as it makes much more sense if there's mutability in the language, which there isn't.
Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @hudem1)
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.
Yes, I see what you mean! What should we do then ? The function take
is in the same case too then.
Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @orizi)
corelib/src/option.cairo
line 812 at r1 (raw file):
}; self.unwrap()
Done.
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.
no - the take is actually ok - as its return value isn't a reference in rust as well.
Reviewed all commit messages.
Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion
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.
Ok yes okay! Makes sense!
Let me know once you've decided whether we want to include this method. In my opinion, the method still fulfills its core purpose - retrieving a value if it exists, or inserting one if it doesn't - so, it can still be useful. But I understand it won't be as versatile as in Rust where we can get a mutable reference to the value inside.
Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @orizi)
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 think it shouldn't - because:
- the current version is too easily replaced with:
let x = opt.unwrap_or_else(f);
opt = Some(x);
x
- If we do have some sort of mutability added - this would block the function from providing it.
Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion
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.
yeah ok I understand! I will close the PR then!
Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @orizi)
Inserts a value computed from a provided closure into the option if it is [
None
], then returns the contained value.Example