Define a primitive data type: Difference between revisions

m
m (→‎{{header|Perl}}: lang tag)
Line 404:
Haskell doesn't have any built-in subrange types. However, it is possible to declare arbitrary types that "behave like" any of the built-in types on the "usual" numeric etc. operations, because these operations are defined by type-classes. So we generalize the task a bit, and first declare a generic container type that supports an additional ''check'' operation. Then, we lift any operation in the base type to the container type, by executing the check after each operation:
 
<lang haskell> {-# OPTIONS -fglasgow-exts #-}
data Check a b = Check { unCheck :: b } deriving (Eq, Ord)
Line 447:
quotRem = lift2p quotRem
divMod = lift2p divMod
toInteger = lift toInteger</lang>
 
Now we can declare the a subrange 1..10 of integer like this:
<lang haskell> newtype TinyInt = TinyInt Int
instance Checked TinyInt Int where
check x | x >= 0 && x <= 10 = Check x
| otherwise = error "Out of range"</lang>
 
In the same way, we could now declare the subtype of the even integers:
 
<lang haskell> newtype EvenInt = EvenInt Int
instance Checked EvenInt Int where
check x | even x = Check x
| otherwise = error "Not even"</lang>
 
Similarly, we could declare the subtype of floating point numbers with restricted exponent, and so on.
 
=={{header|Java}}==