Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking at the value of a TVar in GHCi

Working through Simon Peyton Jones concurrency example, I have the following code:

import Control.Concurrent.STM
import Control.Concurrent.STM.TVar

deposit account amount = do
    bal <- readTVar account
    writeTVar account (bal+amount)

I am trying to test this in GHCi REPL

*Main> checking <- atomically $ newTVar 100
*Main> atomically $ deposit checking 10

How do I verify my checking balance is $110?

I have tried

*Main> checking
*Main> readTVar checking
*Main> balance <- readTVar checking
like image 910
zhon Avatar asked Nov 27 '25 18:11

zhon


1 Answers

atomically $ readTVar checking does what you want. The GHCi REPL automatically executes any IO action that you give it.

like image 136
Venge Avatar answered Nov 30 '25 09:11

Venge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!