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

CHANGE: Rename Rng::gen to avoid conflicting with a keyword in Rust 2024 #1435

Closed
Tracked by #1165
PatchMixolydic opened this issue Apr 9, 2024 · 6 comments · Fixed by #1438
Closed
Tracked by #1165

CHANGE: Rename Rng::gen to avoid conflicting with a keyword in Rust 2024 #1435

PatchMixolydic opened this issue Apr 9, 2024 · 6 comments · Fixed by #1438

Comments

@PatchMixolydic
Copy link

PatchMixolydic commented Apr 9, 2024

Forgive me if this has been posted before, but I couldn't find an issue tracking this.

Summary

RFC 3513, which reserves gen as a keyword for generators in Rust 2024, has been accepted (tracking issue). This conflicts with the naming of Rng::gen. This issue proposes renaming or adding an alias for Rng::gen.

Details

The internal implementation is the easy part: Rng::gen should either be removed or deprecated, and a new method with the same functionality should be added. The difficult part would be deciding on what this new method should be called. This issue is meant to provide an avenue for bikeshedding the new name.

One possibility could be Rng::generate, though this seems slightly unwieldy.

Motivation

In Rust 2024, gen is slated to become a keyword. This means that anyone who calls Rng::gen will have to use a raw identifier:

let x = rng.r#gen::<u32>();

// Alternatively
let y: u32 = rng.r#gen();

This is suboptimal for several reasons: it requires the use of a somewhat obscure feature, it is much less readable at a glance, it's more difficult to type, it runs the risk of turning into sigil soup (especially when using a turbofish), etc. Picking a different name would improve the UX of this common method in Rust 2024 and beyond.

Alternatives

Do nothing

Rng::gen will stay as it is, and anyone who uses Rust 2024 and above will have to call it using a raw identifier. This is suboptimal for the reasons detailed in the Motivation section, but avoids the resulting ecosystem churn from renaming a common method.

Convince the Rust language team to adopt a different keyword

As can be seen on the tracking issue, whether gen is the right keyword for generators is an unresolved question. Since rand uses gen prominently, it might be worthwhile for rust-random to weigh in if it hasn't already.

Remove Rng::gen

As far as I can tell, Rng::gen is a convenience alias for Rng::sample(Standard). This implies that Rng::gen could be removed entirely. However, this would cause just as much churn as renaming Rng::gen, and maintainers would probably want to understand what Standard and Rng::sample are before switching to them. This would also create a pain point for newcomers, as they'd no longer have an obvious starting point for generating an arbitrary random value for a given type.

@newpavlov
Copy link
Member

Renaming it to something like gen_value looks like a good solution.

@dhardy
Copy link
Member

dhardy commented Apr 10, 2024

Thanks for raising this with us @PatchMixolydic. This gives us strong motivation for actually getting a new release out this year!

In other languages, it can be common practice to do something like let sample = rng.gen() * 2.0 + 1.0, but we already have gen_range. So, is this method actually used much? We could rename to standard_sample or deprecate (and the same for gen_iter, leaving only gen_range using gen_*).

@dhardy
Copy link
Member

dhardy commented Apr 10, 2024

There are a lot of offending uses of gen in this repo:

  • Bernoulli compares a standard sample against its probability
  • Uniform float and integer impls start from a standard sample
  • Standard impls for signed and non-zero ints over impls for unsigned ints
  • Tuple, array, Option and Wrapping impls of Standard
  • Fill impls
  • seq::index::sample_efraimidis_spirakis uses a standard f64 sample
  • rand::random
  • Quite a few tests
  • Several doc examples (toy examples, so unimportant)

Probably then quite a few users of this library will be impacted.

@vks
Copy link
Collaborator

vks commented Apr 10, 2024 via email

@TheIronBorn
Copy link
Collaborator

Reusing the sample keyword (like let x: u8 = rng.sample();) might be more intuitive, at least for Distribution. Though the overlapping traits/etc might also be more confusing

@dhardy
Copy link
Member

dhardy commented Apr 11, 2024

Rng::random could be a good alternative name

Best choice yet

Reusing the sample keyword

We already have Rng::sample though: rng.sample(Standard)

@dhardy dhardy mentioned this issue Apr 11, 2024
24 tasks
vks added a commit to vks/rand that referenced this issue Apr 26, 2024
This anticipates `gen` being a keyword in Rust 2024. A deprecated alias
for `Rng::gen` stays in place to make the transition easier.

Fixes rust-random#1435.
vks added a commit to vks/rand that referenced this issue Apr 29, 2024
This anticipates `gen` being a keyword in Rust 2024. A deprecated alias
for `Rng::gen` stays in place to make the transition easier.

Fixes rust-random#1435.
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 a pull request may close this issue.

5 participants