[Haskell-beginners] global variables

MAN elviotoccalino at gmail.com
Wed Aug 11 16:44:42 EDT 2010

There are no global variables in Haskell... You can have the names in
the widest namespace, but there's no modification allowed. Whether you
define your connection in the main, or at top (module) level, when you
pass it to a function, it'll be copied.
In your example code your defining a function 'fn' which has this

  fn :: Connection -> a -> IO b

But the Connection that 'fn' gets is a parameter, not a global.
Ertugrul's approach is the correct one: you define your functions
letting the transformers take care of the plumbing, and the params get
passed around for you.

El mié, 11-08-2010 a las 12:54 -0700, prad escribió:
> i'm curious about the best way to deal with conn to make db connections.
> i define it in main like this, then when i want to use it in a function
> i have to pass it as a parameter:
> main = do
>     conn <- connectPostgreSQL ...
>     fn conn something
> fn conn x = ...
> now conn is a global variable i guess because it is defined in main,
> but perhaps main isn't considered a global namespace? i recall in
> python a global variable could be accessed by functions, but in php
> they can't unless you use "global" within the function.
> so is this the correct way to handle such things? right now it seems
> to be the only way in haskell.

More information about the Beginners mailing list