test :: (String, Bool)
test = runContext $ do
a <- newRef "foo"
b <- newRef False
restore <- save
writeRef a "bar"
writeRef b True
when someCondition restore
x <- readRef a
y <- readRef b
return (x, y)
If someCondition, the existing state is restored and test returns ("foo", False)
otherwise nothing special happens and test returns ("bar", True)
Also each reference has a unique key that can make it much easier to convert structures that use STRefs to pure stuctures.
Reads, writes and creating refs are still constant time operations (with only a very very small overhead) and garbage collecting behavior should be the same as with regular STRefs.
My implementation is here:
What do you think? Any suggestions?
Does anything like this already exist in hackage?
Does this seem useful to other people besides me? :)
Any glaring purity issues that I overlooked?
Thanks for your input,
- Job