[Haskell-cafe] "Casting" newtype to base type?
Frerich Raabe
raabe at froglogic.com
Mon Jul 1 17:21:03 CEST 2013
Am 7/1/2013 5:07 PM, schrieb Vlatko Basic:
> 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
What you really want is
newtype IOS = IOS (IO String)
I.e. a IOS value "wraps" an IO String.
> data P = P {
> a :: String,
> b :: String,
> c :: IOS
> } deriving (Show, Eq)
>
> but now when I try to set 'c' field in
>
> fun :: FilePath -> P -> IO P
> fun path p = do
> b <- doesFileExist path
> ...
> return $ p {c = readFile path}
>
> I get error
> Couldn't match expected type `IOS' with actual type `IO String'
>
> which is correct.
>
> So, the question is:
>
> Is it possible to somehow "cast" 'IO String' to 'IOS'
With the change to your 'newtype', you could use
return $ p {c = IOS (readFile path)}
instead.
--
Frerich Raabe - raabe at froglogic.com
www.froglogic.com - Multi-Platform GUI Testing
More information about the Haskell-Cafe
mailing list