[Haskell-cafe] Overloading
Peter Caspers
pcaspers1973 at gmail.com
Sun Mar 10 15:35:23 CET 2013
Hi Daniel,
> 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
yes, I understand (Maybe seems the equivalent of c++'s boost::optional<T>).
>> -- 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 | ...
ok, I will try to change my code in that direction. The idea is clear.
> I would use the descriptive names but leave out the 'date', because you could still have:
>
> import qualified Date
>
> Date.fromSerialNumber
>
also clear, yes. I think I have a better starting point now. Not
impossible that I will come back later with further questions :-)
Thank you for your help
Peter
More information about the Haskell-Cafe
mailing list