[Haskell-cafe] "Casting" newtype to base type?

Vlatko Basic vlatko.basic at gmail.com
Mon Jul 1 20:17:07 CEST 2013


I'm experimenting. The IO field is just a helper field, so it shouldn't have any 
consequences.


-------- Original Message  --------
Subject: Re: [Haskell-cafe] "Casting" newtype to base type?
From: Tom Ellis <tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk>
To: haskell-cafe at haskell.org
Date: 01.07.2013 17:24

> On Mon, Jul 01, 2013 at 05:07:00PM +0200, Vlatko Basic wrote:
>> Hello Cafe!
>>
>> I had a (simplified) record
>>
>>    data P = P {
>>      a :: String,
>>      b :: String,
>>      c :: IO String
>>      } deriving (Show, Eq)
>>
>> but to get automatic deriving of 'Show' and 'Eq' for 'data P' I have
>> created 'newtype IOS' and its 'Show' and 'Eq' instances
>>
>>    newtype IOS = IO String
>>    instance Show (IOS) where show _ = "(IO String) function"
>>    instance Eq   (IOS) where _ == _ = True
>
> An Eq instance for something containing IO is bound to lead to puzzlement
> somewhere down the line.  I think you're better off defining something like
>
>      data P_lesser = P_lesser {
>          a_lesser :: String,
>          b_lesser :: String
>      } deriving (Show, Eq)
>
>      to_lesser p = P_lesser (a p) (b p)
>
> and just factoring everything through "to_lesser" when you want to compare
> or show.
>
> Tom
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



More information about the Haskell-Cafe mailing list