> 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?<br><br>Why would that be badly designed? And why would that be more OO? IMO it is a perfectly suited usage of type classes.<br><br>> Here's how I do it:<br>
<br>
> data InteractiveState = InteractiveState {<br>
> state_read :: IO Command<br>
> , state_write :: Result -> IO ()<br>
> }<br><br>Well, it's pretty much the same thing, except you explicitely carry a value containing your methods instead of simply carrying a type. Plus it delays the resolution of which function will be called to the execution.<br>
With typeclasses, it will be determined statically, no need to carry the functions.<br><br><br><div class="gmail_quote">2011/6/7 Arnaud Bailly <span dir="ltr"><<a href="mailto:arnaud.oqube@gmail.com">arnaud.oqube@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hello,<br>
In a recent thread, it has been asserted that defining type class is something you seldom need when programming in Haskell.<br><br>There is one thing that as non-professional Haskell programmer I found
type-classes useful for: Testing. This is probably very OO and is pretty
much influenced by what I read in RWH but I find useful to define TC
that abstract away from low-level interactions with other code, possibly
IO related, in such a way that I can have one implementation for
testing and one implementation for real work that is wired in caller
context. This is what is called "mockist" TDD in some circles: Write
code that expresses what it needs in its own terms, then implement
"glue" to the code that provide the concrete behaviour. <br>
<br>For example, while designing some program (a game...) I defined a type class thus:<br><br>> class (Monad io) => CommandIO io where<br>> readCommand :: io Command<br>> writeResult :: CommandResult -> io ()<br>
<br>Then I defined in a module Commands.IO :<br><br>> instance CommandIO IO where<br>> readCommand = do input <- getLine <br>> ...<br>> writeResult r = putStrLn $ show r<br><br>and for the purpose of testing I defined in some test module:<br>
<br>> instance CommandIO (S.State ([Command],[CommandResult])) where<br>> readCommand = do ((c:cs),rs) <- S.get<br>> ....<br>> writeResult r = do (cs,rs) <- S.get<br>> ...<br><br>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? <br>
<br>Regards,<br><font color="#888888">Arnaud<br><br><br>
</font><br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br>