[Haskell-beginners] data constructor with IO type

Brent Yorgey byorgey at seas.upenn.edu
Tue Feb 24 12:30:23 EST 2009


On Tue, Feb 24, 2009 at 08:04:33PM +0300, Daneel Yaitskov wrote:
> 
> Hi All,
> 
> I'd like to know does it exist a method which allow to construct the TestB
> type without a temporary variable such as the TestA constructor.
> 
> ---
> module Main where
> 
> import Monad
> import Control.Concurrent
> data TestA = TestA (MVar Bool)
> xGetA (TestA x) = x
> data TestB = TestB { xGetB ::  MVar Bool } 
> 
> ---
> main =
>  do tmp <- newMVar False
>    let t = TestB { xGetB  = tmp } in
>        do vt <- takeMVar t
>           putStrLn ("HELLO" ++ show vt)
> ---
> 
> main = do
>  t <- liftM TestA  (newMVar False)
>  vt <- takeMVar (xGetA t)
>  putStrLn ("HELLO" ++ show vt)
> 
> ---
> 
> Daneel Yaitskov

Hi Daneel,

Note that record syntax like

> data TestB = TestB { xGetB ::  MVar Bool }

only adds capabilities; you can still use TestB as if it was defined
without record syntax, like TestA.  So you are not required to use the 

  TestB {xGetB = tmp} 

syntax to create a TestB, you could also just say 'TestB tmp'.  So the
second code example should work fine if you just replace all the 'A's
with 'B's.  

Does this answer your question?  I must admit that I am not entirely
sure what you are asking, so if this doesn't address your question
feel free to clarify.

-Brent


More information about the Beginners mailing list