Constrained ordinals

Is it possible to create "constrained ordinal" types ala pascal?

e.g. say you need numbers only 1..100 and anything else to compile error on literals and runtime error on out of range. Internally it'd be stored and behave exactly like u8 with the added constraints.

This could be really useful for coordinate systems and such where certain values are out of range. Catching any constant/compile time violations is a nice bonus too.

Not until const-generics becomes more powerful, you can track const-generics here

Didn't realize that's a use case for const generics but it makes sense now. Makes me a bit sad, seems like rust is desperately missing them for a lot of things. I wish const generics got the attention of async/await instead...

2 Likes

Me too