
7 Jun
2011
7 Jun
'11
8:32 p.m.
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.