[Haskell-cafe] Rethinking OO idioms

David Roundy droundy at abridgegame.org
Thu Sep 30 05:01:28 EDT 2004


On Wed, Sep 29, 2004 at 08:29:47PM +0000, John Goerzen wrote:
> So I am thinking about a ConfigParser for Haskell.  The first thing that
> occured to me is that Haskell has no OO features, so I'm not sure what
> is the best way to handle the "class" and its various methods.
> 
> The next thing that occured to me is that, unlike OCaml and Python
> classes, Haskell has no mutable variables.  A call like
> config.setOption("main", "initpath", "/usr") in Python -- which alters
> the state of the config object and returns nothing -- would be
> impossible in Haskell (unless perhaps the FiniteMaps are mutable
> somehow?)

I might define just two IO functions:

parseConfig :: FilePath -> IO Config
modifyConfig :: FilePath -> (Config -> Config) -> IO Config

This way, you could do all the modification in pure functional code, which as
Alastair said, would create a "new" Config rather than modifying the
existing one.

Of course, you could also define a

writeConfig :: FilePath -> Config -> IO ()

but then a user of your class could accidentally overwrite a change, if you
had two parts of the code which read the same config, each made separate
changes, and then each wrote their separate changes.
-- 
David Roundy
http://www.abridgegame.org


More information about the Haskell-Cafe mailing list