Was that an open question or did you have a solution in mind?
More open. I saw it land in stable some time back and haven’t gotten around to playing with it. I honestly haven’t done it much, because usually enums are plentywhen there are a finite set of options.
And yeah, I was thinking of runtime checks with const bounds, like this:
pubstructBoundedI32<const MIN: i32, const MAX: i32>(i32);
impl<const MIN: i32, const MAX: i32> BoundedI32<{ MIN }, { MAX }> {
pubconst MIN: i32 = MAX;
pubconst MAX: i32 = MAX;
pubfnnew(n: i32) ->Self {
// or an assertBoundedI32(n.min(Self::MAX).max(Self::MIN))
}
}
I’m not sure how magic Ada gets with things, so maybe it’s a lot nicer there, but I honestly can’t see how it could really improve on handling runtime checks.
Well, I don’t know much about Ada, but it’s typically lauded for all its compile-time checks. Obviously, you can’t compile-time check something when it’s loaded at runtime from e.g. a configuration file, but yeah, I’m guessing that’s probably where it shines, that it uses compile-time checks when possible.
More open. I saw it land in stable some time back and haven’t gotten around to playing with it. I honestly haven’t done it much, because usually enums are plentywhen there are a finite set of options.
And yeah, I was thinking of runtime checks with const bounds, like this:
pub struct BoundedI32<const MIN: i32, const MAX: i32>(i32); impl<const MIN: i32, const MAX: i32> BoundedI32<{ MIN }, { MAX }> { pub const MIN: i32 = MAX; pub const MAX: i32 = MAX; pub fn new(n: i32) -> Self { // or an assert BoundedI32(n.min(Self::MAX).max(Self::MIN)) } }
I’m not sure how magic Ada gets with things, so maybe it’s a lot nicer there, but I honestly can’t see how it could really improve on handling runtime checks.
Well, I don’t know much about Ada, but it’s typically lauded for all its compile-time checks. Obviously, you can’t compile-time check something when it’s loaded at runtime from e.g. a configuration file, but yeah, I’m guessing that’s probably where it shines, that it uses compile-time checks when possible.
I would really like the mythical higher kinded types (which I think covers what Ada does here), but unfortunately we don’t have that yet.