
28 Dec
2008
28 Dec
'08
10:26 a.m.
Colin Paul Adams
I want to declare a type thus:
type Coordinate = (Int, Int)
But the two integers must be confined to the inclusive range 0-11. Can i express that in the type system?
Well, the type system allows you to create your own ranged integer type. Here is a rather inflexible, but working method to do it: newtype Int12 = Int12 Int deriving (Eq, Read, Show) instance Num Int12 where Int12 x + Int12 y | x+y <= 11 = Int12 (x+y) | otherwise = error "Int12 addition out of range" ... Greets, Ertugrul. -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://blog.ertes.de/