sschaef sschaef - 2 months ago 16
Scala Question

Why doesn't Scala have an IO Monad?

I'm wondering why Scala does not have an IO Monad like Haskell.

So, in Scala the return type of method

readLine
is String whereas in Haskell the comparable function
getLine
has the return type IO String.

There is a similar question about this topic, but its answer it not satisfying:


Using IO is certainly not the dominant style in scala.


Can someone explain this a bit further? What was the design decision for not including IO Monads to Scala?

Answer

Because Scala is not pure (and has no means to enforce that a function is pure, like D has) and allows side effects. It interoperates closely with Java (e.g. reuses big parts of the Java libraries). Scala is not lazy, so there is no problem regarding execution order like in Haskell (e.g. no need for >> or seq). Under these circumstances introducing the IO Monad would make life harder without gaining much.

But if you really have applications where the IO monad has significant advantages, nothing stops you from writing your own implementation or to use scalaz. See e.g. http://apocalisp.wordpress.com/2011/12/19/towards-an-effect-system-in-scala-part-2-io-monad/

[Edit]

Why wasn't it done as a lazy and pure language?

This would have been perfectly possible (e.g. look at Frege, a JVM language very similar to Haskell). Of course this would make the Java interoperability more complicate, but I don't think this is the main reason. I think a lazy and pure language is a totally cool thing, but simply too alien to most Java programmers, which are the target audience of Scala. Scala was designed to cooperate with Java's object model (which is the exact opposite of pure and lazy), allowing functional and mixed functional-OO programming, but not enforcing it (which would have chased away almost all Java programmers). In fact there is no point in having yet another completely functional language: There is Haskell, Erlang, F# (and other MLs) and Clojure (and other Schemes / Lisps), which are all very sophisticated, stable and successful, and won't be easily replaced by a newcomer.

Comments