[Haskell-cafe] Non-advanced usage of Type classes

Evan Laforge qdunkan at gmail.com
Tue Jun 7 22:32:34 CEST 2011


> Is this badly designed  code that tries to mimic OO in a functional setting?
> If the answer is yes, how could I achieve same result (eg. testing the code
> that does command REPL) without defining type classes?

Here's how I do it:

data InteractiveState = InteractiveState {
  state_read :: IO Command
  , state_write :: Result -> IO ()
  }

Now when I run it for real, I pass 'InteractiveState getLine putStrLn'
and when I run it for testing, I pass 'InteractiveState (getChan
in_chan) (putChan out_chan)'.

Of course, you have to pass this InteractiveState around, but
hopefully your IO using section is restricted to a small event loop
and threading it through is not a burden.  And there's always StateT.



More information about the Haskell-Cafe mailing list