How to perform data type structure checks

Is there a way for a data type constructor to restrict the creation of an “object” (I don’t know the right term) based on criteria other than the constructor argument types?

For instance:

data UInt = UInt Int --the int must be >= 0

Indeed, I would like to create a data type for rectangular multidimensional lists (in which all the sublist letters have the same length). Would a class or some other method be better suited for this?

+4
source share
1 answer

No, it is impossible to establish what values ​​the user passes to the constructor.

. , Data.Map, Data.Ratio . , , , , :

module UInt
    ( UInt
    , uint
    ) where

data UInt = UInt Int

uint :: Int -> Maybe UInt
uint x | x >= 0 = Just (UInt x)
       | otherwise = Nothing
+7

All Articles