Skip to content

Error handling of distributions::Uniform::new #1195

@dhardy

Description

@dhardy

Currently, Uniform::new will panic on invalid parameters:

  • low >= high (or > for new_inclusive)
  • (float, debug only) non-finite low or high
  • (float) non-finite scale = high - low

Since #581 / #770, all other distributions with fallible constructor return a Result instead of panic (I think).

For: consistency and utility (error checking in derived types)

Against: this is another significant breaking change, when we hope to be getting close to stable (though now is certainly better than later)

Alternative: try_new, try_new_inclusive constructors - but this is inconsistent and results in far too many methods.

Also related: Rng::gen_range uses this. This should probably still panic on failure (note also Rng::fill vs Rng::try_fill; Rng::gen_bool and Rng::gen_ratio which may panic).

I suggest this error type:

enum Error {
    /// Low > high, or equal in case of exclusive range
    EmptyRange,
    /// Input or range (high - low) is non-finite. Not relevant to integer types. In release mode only the range is checked.
    NonFinite,
}

Link: #1165

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-APIBreakage: API

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions