-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Update once_cell 'get_or_init' to 'get_or_init_with' #107184
Conversation
r? @thomcc (rustbot has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
Some changes occurred in src/librustdoc/clean/types.rs cc @camelid Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
Two things also regarding
Less pressure to think about this since they're no longer in #74465 |
@rustbot modify labels: +T-libs-api -T-rustdoc -T-libs -T-compiler r? m-ou-se (just since you're the assignee on the stabilization PR) |
31c8815
to
23608b0
Compare
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt Some changes occurred in src/librustdoc/clean/types.rs cc @camelid Some changes occurred in src/tools/clippy cc @rust-lang/clippy Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
Sorry for the noise MIR/compiler/clippy team |
☔ The latest upstream changes (presumably #106975) made this pull request unmergeable. Please resolve the merge conflicts. |
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.
None of the justifications described above are compelling to me and I would prefer to keep get_or_init
.
Particularly this main reason:
The main reason is it might make sense in the future to re-add a
get_or_init
that takes a value rather than a closure. I kind of doubt it would ever get added (unless we elect to do it now) as it's just easy sugar for.get_or_init_with(|| val)
. However, unless we pick a_with
name for theFnOnce
version now, we wouldn't be able to add it later and keep a consistent naming scheme.
I'd be happy to rule this out now. That's effectively what the team has already done by approving the FCP to stabilize get_or_init
.
I can't imagine that initializing with a value expression instead of a closure would be a common enough use case of get_or_init to justify an extra method dedicated to making it marginally more convenient. Comparing the following scenarios:
// 5% use case?
once_cell.get_or_init(|| val)
once_cell.get_or_init(val) // proposed change: -3 characters
// 95% use case?
once_cell.get_or_init(|| ...)
once_cell.get_or_init_with(|| ...) // proposed change: +5 characters
I don't really see this coming out in favor of a rename. The API is more convenient much more of the time prior to this PR.
Does the libs team have a doc that outlines how to pick function names? If not, it could be valuable to write down the thought process that leads to whatever decision is taken here so it can be referred to in the future. I personally don't see the
|
23608b0
to
656f2db
Compare
- 'get_or_init' -> 'get_or_init_with' - 'get_or_try_init' -> 'get_or_try_init_with'
656f2db
to
f3d072e
Compare
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.
Un-approving this for now, because we can't stabilize this unless all libs-api members are on board with it. ^^
(I personally don't feel strongly about this. I can see how _with
can be a bit more consistent, but get_or_init
also seems fine to me.)
3 people have said the word consistency, but I don't understand a consistency argument for this PR either. "Put |
I agree with that. (See also I could see there is some value in treating (From the discusson on #105587 I got the impression that |
The reason I brought this up was because it hadn't been discussed much, as @matklad pointed out in #105587 (right at the end of FCP). The final place that naming discussion ever came up is here #74465 (comment) where I assumed the 5 upvotes were showing mild +1 for
All in all I'm quite OK closing this now that there's been some discussion, if there isn't strong motivation for the change (unless @BurntSushi has anything to say as the third RFC approver). If this is the case, then I think the stabilization PR is ready for merge. |
Looking at the other stdlib APIs that end in
So then this makes sense for keeping it |
Which just hadn't been discussed before - hence bringing it up here :) |
I like |
As a procedural observation, naming was (and still is) an unchecked [ ] concern on the corresponding tracking issue without resolution more explicit than “let’s roll with the historical option”, so, for an outside observer, the two worlds looked equivalently:
|
Potentially blocks #105587
I hate myself for bikeshedding on a stabilization PR I care about, but I think
get_or_init_with
might be a better name forget_or_init
.The main reason is it might make sense in the future to re-add a
get_or_init
that takes a value rather than a closure. I kind of doubt it would ever get added (unless we elect to do it now) as it's just easy sugar for.get_or_init_with(|| val)
. However, unless we pick a_with
name for theFnOnce
version now, we wouldn't be able to add it later and keep a consistent naming scheme.Other points are that the near cousin
RefCell
hasreplace(&self, T)
andreplace_with<F: FnOnce...>(&self, f: F)
, so changing the name would make this consistent. And the_with
naming pattern is well established anyway, as I mention here #105587 (comment)Downsides of changing are, of course, potential confusion for anyone using the crate, and the slightly longer name.
I'm not tied to the
get_or_init
portion specifically, and matklad mentioned some other options here #74465 (comment).cc @matklad