Skip to content
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

Closed

Conversation

hudem1
Copy link
Contributor

@hudem1 hudem1 commented Jan 14, 2025

Inserts a value computed from a provided closure into the option if it is [None], then returns the contained value.

Example

let mut x = Option::None;
let y = x.get_or_insert_with(|| 5);
assert_eq!(y, 5);

let mut x = Option::Some(2);
let y = x.get_or_insert_with(|| 5);
assert_eq!(y, 2);

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Collaborator

@orizi orizi left a 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    
            }
        }

Copy link
Collaborator

@orizi orizi left a 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)

Copy link
Contributor Author

@hudem1 hudem1 left a 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.

Copy link
Collaborator

@orizi orizi left a 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

Copy link
Contributor Author

@hudem1 hudem1 left a 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)

Copy link
Collaborator

@orizi orizi left a 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:

  1. the current version is too easily replaced with:
let x = opt.unwrap_or_else(f);
opt = Some(x);
x
  1. 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

Copy link
Contributor Author

@hudem1 hudem1 left a 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)

@hudem1 hudem1 closed this Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants