data constructor with IO type

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

data constructor with IO type

Daneel Yaitskov

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

Reply | Threaded
Open this post in threaded view
|

data constructor with IO type

Brent Yorgey-2
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
Reply | Threaded
Open this post in threaded view
|

Re: data constructor with IO type

Daneel Yaitskov
Brent Yorgey <[hidden email]> writes:

>
> 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
I know about it. I mean how to save names of arguments.
Because usually data structures is complex and they contain many fields.
Method which doesn't the names of the arguments requires to give the
values for all members. Sometimes some subsets of one structure calculate
 themselves at other places or structure contains many fields. One part of
them get themselves from the pure functions and other do from the action
functions.

Daneel Yaitskov