-
-
Notifications
You must be signed in to change notification settings - Fork 437
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
rand_distr: Use Result
instead of panics
#770
Conversation
This also fixes a few bugs about invalid parameters in the documentation and the code. Refs rust-random#581.
What is the plan for the distributions in |
Thanks for getting this started. Do you think #100 has any effect on this? I haven't looked into it recently but some method for supporting lower precision / other types would be nice to have. |
I think #100 is orthogonal. |
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.
These guards don't catch NaN values, which do sometimes occur in numerical applications. So better I think to replace k <= 0.0
with !(k > 0.0)
etc.
That way you need two checks. Infinite values are often acceptable, in which case a single check like |
Maybe there should be an error variant for NaN in arguments? |
That's one option, but needs a second round of testing. Is it worthwhile? |
@dhardy I just changed the checks as you suggested. |
Note that the error variants here are not yet public, so we could worry about the details later. Or I can still make them public for this PR. |
Interesting point about the error types not being public — why isn't this a compile error (private type in public API)? Also note that #775 might remove the need for the current error in |
The type is public, but its variants are not (similar to a public struct with private members). You can still match on it with
This is fine, if we keep the variants private for now we can revisit this later without breakage. |
Am I reading something wrong? In I also don't see anything making the Edit: I suggest making these variants public anyway. Most users won't have a reason to depend on the variants, and tricks like |
You are right, the errors are all private. I expected that the modules for the distributions are public, but they are all private.
Will do! |
What do you think, are maximally-specific error types like this more useful than a single generic Another point is that with the export renames, the API doc shows e.g. Having a single BTW does this have any overlap with #771? Possibly not since we really don't need this to implement |
I'm not a fan of mixing all variants in a huge enum, but I think as single On the other hand, I like how the error types currently serve as documentation for the possible failures.
I could rename the errors or make the modules public and not re-export them. |
Is there some reason or just style? It does avoid the question of whether to add a hidden variant.
So long as the internals are private (allowing us to add a code later), this serves our needs sufficiently. |
It means that the error types are no longer self-documenting, because some variants are unused by a specific distribution, so we have to move the documentation into the distribution's constructor. Also, we have the same problem with the hidden variant, because we still might want to add new variants. However, it is actually quite similar to
Yes, the internals would be private and it would just be used to print the error messages. Otherwise it would behave like |
Agreed. I don't think there's much reason to match on error variants so lets switch to |
Lets take this as-is for now, but revisit the error types soon (at least add some |
This also fixes a few bugs about invalid parameters in the documentation
and the code.
The distributions in
rand
are untouched. In particular,Uniform
and similar still panic.Refs #581.