[Haskell-cafe] user type declarations in Haskell
u.stenzel at web.de
Thu Jun 22 08:16:04 EDT 2006
Vladimir Portnykh wrote:
> I am trying to define the following types
> data MyStringType a = String deriving (Eq, Ord, Show)
> data QADouble a = Double deriving (Eq, Ord, Show)
These are not what you think they are. MyStringType has a phantom type
parameter and only one value, which is the constant String (but not of
type String). What you actually meant can only be guessed, and I'm not
> So HType can represent strings or doubles.
> later I want to do something like the following:
> let a1 =QADouble 1
> let a2 =QADouble 2
> let a3 = a1 + a2
data HType = QADouble Double | QAString Double
> First, it is not working because Haskell complains about a3. it does not
> know how to calculate it.
What did you expect? What's the sum of a Double and a String if not an
error? You have to define (+), which is already defined. Use some
other name and give a definition:
QADouble x `plus` QADouble y = ...
QADouble x `plus` QAString y = ...
QAString x `plus` QAString y = ...
QAString x `plus` QADouble y = ...
Lieber vom Fels zertrümmert als bei einer Frau verkümmert.
-- aus einem Gipfelbuch
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20060622/0dbff813/attachment.bin
More information about the Haskell-Cafe