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

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Tue Jul 2 15:25:25 CEST 2013


On Tue, Jul 02, 2013 at 03:03:08PM +0200, Vlatko Basic wrote:
> Is there a nicer way to extract the 'IO String' from 'IOS',
> without 'case' or without pattern matching the whole 'P'?
> 
> newtype IOS = IOS (IO String)
> data P = P {
>   getA :: String,
>   getB :: String,
>   getC :: IOS
>   } deriving (Show, Eq)
> 
> 
> getC_IO :: P -> IO String
> getC_IO p =
>   case getC p of
>     IOS a -> a
> getC_IO (P _ _ (IOS a)) = a

How about

    unIOS :: IOS -> IO String
    unIOS (IOS a) = a

    getC_IO :: P -> IO String
    getC_IO = unIOS . getC

Tom



More information about the Haskell-Cafe mailing list