Hi all,<br>I have a tuple inside a TVar : <br><font size="1"><span style="font-family: courier new,monospace;">> type MySTM = TVar (Int,Int)</span></font><br><br>Whenever I want to edit/read 'a' or 'b' I find myself writing :<br>
<font size="1"><span style="font-family: courier new,monospace;">> editFunction mySTM = do</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(a',b') <- readTVar mySTM</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">dostuff a' </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">...</span></font><br><br>This is boilerplate stuff, so I decided to write some accessors. So far I have :<br>
<font size="1"><span style="font-family: courier new,monospace;">> getA , getB :: MySTM -> STM Int</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">> getA mySTM = do </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">> (a',b') <- readTVar mySTM</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">> return a'</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">> getB mySTM = do</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">> (a',b') <- readTVar mySTM</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">> return b'</span></font><br>
<br>I want to be able to use these accessors like so:<br><font size="1"><span style="font-family: courier new,monospace;">> doSomethingWithA mySTM = do</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">> case (getA mySTM) of </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">> 1 -> doStuff</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">> 0 -> doSomethingElse</span></font><br>
<br>But getA returns an STM Int, so I still have to do a :<br><font size="1"><span style="font-family: courier new,monospace;">> doSomethingWithA = do</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">> a' <- (getA mySTM</span>)<br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">> case a' of </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">> 1 -> doStuff</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">> 0 -> doSomethingElse</span></font><br><br>This doesn't really save me a lot of boilerplate. What is the best way of writing a function that just returns my values do I can work with them in the STM monad without unpacking them all the time?<br>
<br>Thanks ,<br>Deech<br><br><br>