[Haskell-cafe] Encapsulation in Haskell

Tomasz Zielonka tomasz.zielonka at gmail.com
Fri Dec 23 02:46:42 EST 2005


On Thu, Dec 22, 2005 at 01:53:34PM -0800, Greg Woodhouse wrote:
> The trick, though, is that objects are stateful things. If you want to
> program a binary search tree, then insertion ought to return an
> entirely new tree, just as if objects were all immutable, and
> operations on those objects simply creaqte new objects exactly like the
> existing ones (except for the one change). I've thought about trying to
> model this using monads and actions, but I'm not sure that's the best approach.

Use lexical closures. Initialize the state, variables, threads, etc, and
then return (= expose) only the "methods", ie. monadic actions. Use the
fact that both functions and IO actions are abstract (opaque?).

Example:

    initObject = do
        var <- newVar
        let
          inc = do
            x <- readVar var
            writeVar var (x + 1)
          get = do
            readVar var
        return (inc, get)

Best regards
Tomasz

-- 
I am searching for a programmer who is good at least in some of
[Haskell, ML, C++, Linux, FreeBSD, math] for work in Warsaw, Poland


More information about the Haskell-Cafe mailing list