Count how often a menu has been selected

Hi list, using gtk2hs I would like to count how often a Menu has been selected (well, not really, but it kind of breaks down to this): let a = 0 onActivateLeaf menuAddOne $ do let a = a+1 print a This leads to a stack overflow. Obviously a=a+1 is iterated over and over again. But how can I add something each time the menu is activated? (Actually I want to open a FileDialog and add the contents of a file to a list but for now, counting would be great). Thanks to everyone for reading, Bernhard

On Sat, Jul 25, 2009 at 01:10:02PM +0200, Bernhard Lehnert wrote:
let a = 0 onActivateLeaf menuAddOne $ do let a = a+1 print a
If you want variables in the impure sense, use IORefs. import Data.IORef ... a <- newIORef 0 onActivateLeaf menuAddOne $ do modifyIORef a (+1) print a -- Felipe.

Bernhard Lehnert wrote:
Hi list,
using gtk2hs I would like to count how often a Menu has been selected (well, not really, but it kind of breaks down to this):
let a = 0 onActivateLeaf menuAddOne $ do let a = a+1 print a
This leads to a stack overflow. Obviously a=a+1 is iterated over and over again. But how can I add something each time the menu is activated? (Actually I want to open a FileDialog and add the contents of a file to a list but for now, counting would be great).
Thanks to everyone for reading, Bernhard
I would suggest having a look at Control.Concurrent.MVar http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurren... Best, Thomas

On Sat, Jul 25, 2009 at 10:50:01AM -0400, Thomas Friedrich wrote:
I would suggest having a look at Control.Concurrent.MVar
Probably he doesn't need MVar here because Gtk+ guarantees that all GUI code is executed on one thread, allowing you to avoid the overhead of an MVar. -- Felipe.
participants (3)
-
Bernhard Lehnert
-
Felipe Lessa
-
Thomas Friedrich