[Haskell-cafe] Overloading
Peter Caspers
pcaspers1973 at gmail.com
Sun Mar 10 12:10:01 CET 2013
>> In C++ it is perfectly normal to have overloaded functions like
>>
>> f : Int -> Int -> Int
>> f : Int -> Char -> Int
> Something that may not be obvious about Haskell is that
> Haskell does NOT have overloaded functions/operators at all.
thanks, this was the core of my question. So by example, if I define a
Date type as
data Date = Date Int deriving Show
representing a date by its serial number and want two constructors
(conditions are only examples here)
-- smart constructor with serialNumber
date serialNumber
| serialNumber > 0 = Date serialNumber
| otherwise = error ("invalid serialNumber " ++ show serialNumber)
-- smart constructor with day month year
date2 day month year
| month >= 1 && month <=12 = undefined
| otherwise = error ("invalid month " ++ show month)
there is no way of naming both functions date (instead of date2 above,
which compiles), right ? I still think the basic reason is that
date 5
would then either refer to the first constructor (i.e. representing a
date with serial number 5) or a partial application of the second
constructor (i.e. representing a function taking month and year and
returning the date "5th month, year").
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 ?
Or would you do it differently from the start ?
Thank you
Peter
More information about the Haskell-Cafe
mailing list