I want to define own data type, say a Digit
, which would have definite type of values ( 0 to 9
) and I'm struggling with defining this in a way it's practical. What's the best way (design pattern) to do this?
I'm doing this in Scala, which might provide some advantages, but I'm also interested in general approach.
If instantiating Digit
via Int
constructor ( var d = new Digit(1)
), how to handle "out of bounds" safety in compile type? Is creating subclasses ( var d: Digit = new Digit1()
) a way?
Additional question: How to handle large sets of values - e.g. letters come to mind first?