Hi all,

I've been doing some GUI programming recently, using wx. To help manage dependencies between state and UI elements, I looked for a Haskell version of the Observer design pattern, and I found an implementation written by Bastiaan Heeren of ou.nl [1].

It pretty much did what I wanted, though I made a few changes along the way. Since this seems to be of general use (ie beyond wx), I've proposed to Bastiaan that I package it up for release on hackage, and he's happy for me to do so. Before I do so, I thought I'd ask for comments.

The code is on github: http://github.com/gimbo/observer.hs where we have:

    Control.Observer - a typeclass for observable objects.
    Control.Observer.Synchronous - an implementation based on IORefs.

This is essentially the same as Bastiaan's original, except I've changed the names, split it into two modules, commented it, added one or two small things, and Cabalised it.

I've also made a branch where Control.Observer.Synchronous uses MVars instead of IORefs, in an attempt to achieve thread safety:


Now, before I make a hackage release:

0. Is this completely insane, in a Haskell setting? Are there better ways to do this that aren't laden with OO-worldview baggage?

1. Does anyone have any comments, on either version?

2. In particular, is the MVar version sensible? I'm aiming for mutual exclusion between threads. I _think_ I've got it, but I'm perhaps not familiar enough with the semantics of MVar to be certain. Advice appreciated. If it _is_ sensible, then is there any reason not to just use this, and discard the IORef version?

The current implementation is synchronous, in that any observer functions are called immediately and synchronously (and in the same thread as the change of subject value). I'm pondering extending the package with an asynchronous version where the update just trips a flag, and the observer function picks this up later - possibly in another thread. The idea there is to help in cases where certain operations have to be in a particular thread. But this will mean a change to the typeclass too, I guess - or the addition of another one for observers themselves. Again, any thoughts?



[1] http://www.cs.uu.nl/wiki/bin/view/Afp0607/ExerciseWXHaskell

Haskell-Cafe mailing list

Reply via email to