[Haskell-cafe] Re: Sometimes I wish there was a global variable

Felipe Lessa felipe.lessa at gmail.com
Sun Mar 22 19:27:51 EDT 2009


On Sun, Mar 22, 2009 at 08:10:28PM -0300, Rafael Cunha de Almeida wrote:
> Doing it like that I could have all the definitions in one module, but
> it doesn't solve another important problem: keyboardMouse and display
> functions have to have as many parameters as there are IORefs.

You may always create a new data type.

> Whereas in Haskell I would have something like
> 	keyboard var1 _ LeftKey = ...
> 	keyboard _ var2 RightKey = ...
> If I wanted to add a new IORef I'd have to do something like:
> 	keyboard var1 _ _ LeftKey = ...
> 	keyboard _ var2 _ RightKey = ...
> 	keyboard _ _ var3 RightKey = ...
> So, I had to change all previous definitions.



Initial code:

> data Vars = Vars {var1 :: {-# UNPACK #-} !(IORef Type1)
>                  ,var2 :: {-# UNPACK #-} !(IORef Type2)}
>
> keyboard v LeftKey  = ... (var1 v) ...
> keyboard v RightKey = ... (var2 v) ...

Modified code:

> data Vars = Vars {var1 :: {-# UNPACK #-} !(IORef Type1)
>                  ,var2 :: {-# UNPACK #-} !(IORef Type2)
>                  ,var3 :: {-# UNPACK #-} !(IORef Type3)}
>
> keyboard v LeftKey  = ... (var1 v) ... -- unchanged
> keyboard v RightKey = ... (var2 v) ... -- unchanged
> keyboard v OtherKey = ... (var3 v) ...

Note that the UNPACKs aren't really necessary.

--
Felipe.


More information about the Haskell-Cafe mailing list