Possible Duplicate:
How to make a type with restrictions
Is it possible in Haskell to create a type for example “Name” which is a String but containing no more then 10 letters?
If not how can I forbid to create a Person with to long name (where Person is defined like that: data Person = Person Name).
Maybe it is not important at all, maybe that kind of problems should be solved in Haskell in a different way?
Don’t export the constructor from the module where you define the type, and instead export a “smart constructor”:
So you’re concerned that if you previously had code that didn’t require names to be limited to ten characters, you can’t just drop in
nameFromStringas it has typeString -> Maybe Nameinstead ofString -> Name.First, if you really want to throw an exception, you can define
and use that instead.
Second, throwing an exception is sometimes the wrong thing to do. Consider
Rewriting this to use exceptions would result in more complex code.