-
Notifications
You must be signed in to change notification settings - Fork 430
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
Choose should panic #924
Comments
So you're saying that Since this concerns a breaking change to an existing API, such a change would need quite a significant justification to be enacted. |
Sure, but rand is still far from 1.0, so I thought about expressing this opinion before it'd be too late. I'd be up to check out what most crates that use choose treat the None case as, if the majority simply unwrap I don't see this as an unreasonable change. match slice.choose(&mut rng){
Some(v) => do_things_with_sample(v),
None => do_other_thing();
} Is much less expressive than if !slice.is_empty() {
do_things_with_sample(slice.choose(&mut rng));
}
else{
do_other_thing();
} |
Even if the more subtle API were better (it's not), it wouldn't be worth a breaking change to a crate with 3000 direct dependents; SemVer doesn't enter into it. |
@mgostIH BTW, you can do this: if let Some(choice) = slice.choose(&mut rng) {
do_things_with_sample(choice);
}
else{
do_other_thing();
} Although the idiomatic way to do that example would be: slice
.choose(&mut rng)
.map(do_things_with_sample)
.unwrap_or_else(|| do_other_thing()); |
@mgostIH I think your example is an argument for the current API, not against it. In my opinion, the example given by @kazcw using the current API is preferable, because it does not generate any panicking code. The advantage of the API you proposed is saving an I would prefer to keep the API as is, for the following reasons:
In my opinion, any of these outweigh the advantage of saving an |
I think we should look into how |
Somehow @newpavlov commented just as I clicked the "close" button. However, I believe the above arguments are sufficient not to re-open this; in particular:
|
I think the API of SliceRandom::choose should panic instead of returning None to provide a cleaner API, since the case of having an empty slice seems very unlikely. Often times, even in other languages, I've seen random sampling used with already known arrays, but I can understand that there the model of error handling may be different, so I'd be happy to see more discussion about this.
The text was updated successfully, but these errors were encountered: