In my Data
data User = User { uRank :: Int, uProgress :: Int }
I want to limit uRank
to a list of values [-1, 1, 3], for example.
How do I do this?
Defining a small sum type is the best answer for this specific question. You can also use newtype
with smart constructors to achieve this effect.
newtype Rank = UnsafeMkRank { unRank :: Int }
mkRank :: Int -> Maybe Rank
mkRank i
| i `elem` [-1, 1, 3] = Just (UnsafeMkRank i)
| otherwise = Nothing
Now, provided you only use the safe mkRank
constructor, you can assume that your Rank
values have the Int
values you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With