It would be really nice to be able to do something like this:
> data Action = Code | Eat | Sleep deriving (Bounded, Enum)
> randomR :: IO Action
Eat
The implementation for (Enum a, Bounded a) => can cheat a little to get a valid range (psuedo, for randomRIO since it's easier/shorter to show):
let lower = fromEnum (minBound :: a)
let upper = fromEnum (maxBound :: a)
return $ toEnum <$> randomRIO (lower, upper)
Note the usage of the underlying instance for Int to select a constructor.
It would have been magical if we could have just a Enum a => instance, but I can't think of a way to do it.
It would be really nice to be able to do something like this:
The implementation for
(Enum a, Bounded a) =>can cheat a little to get a valid range (psuedo, for randomRIO since it's easier/shorter to show):Note the usage of the underlying instance for
Intto select a constructor.It would have been magical if we could have just a
Enum a =>instance, but I can't think of a way to do it.