[Haskell-cafe] Overloading
Daniel Trstenjak
daniel.trstenjak at gmail.com
Sun Mar 10 12:44:07 CET 2013
Hi Peter,
> -- smart constructor with serialNumber
> date serialNumber
> | serialNumber > 0 = Date serialNumber
> | otherwise = error ("invalid serialNumber " ++ show serialNumber)
Instead of raising an error it's more secure to return a Maybe value.
date :: Int -> Maybe Date
date serialNumber
| serialNumber > 0 = Just $ Date serialNumber
| otherwise = Nothing
> -- smart constructor with day month year
> date2 day month year
> | month >= 1 && month <=12 = undefined
> | otherwise = error ("invalid month " ++ show month)
To increase type safety it's a good idea to use as much explicit data
types instead of Int values as possible:
data Month = January | ...
> If this is the case, what would be the natural Haskell way of
> organizing the smart constructors ? Just number them as above ? Or
> naming them
> dateFromSerialNumber, dateFromDayMonthYear ?
I would use the descriptive names but leave out the 'date', because you could still have:
import qualified Date
Date.fromSerialNumber
Greetings,
Daniel
More information about the Haskell-Cafe
mailing list