Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the IO Haskell Monad equivalent in Scala standard API?

I know there are some almost identical implementations in Scalaz of IO operators like putStrLn :: String -> IO () and getLine :: IO String but I mean about Scala standard API why there aren't such equivalents? I know Scala is not a pure language as Haskell and there is side effect but I consider that this datatype is so descriptive and useful. I don't know if Try, Option or Either would do the job.

like image 677
salc2 Avatar asked Jun 23 '17 13:06

salc2


2 Answers

Scala's standard library has no IO monad. Libraries like Monix, Scalaz, FS2, and now Cats provide IO monads (or Task with concurrency support). If you want to haskell type programming you will need one of these libraries, especially Cats or Scalaz which also other useful tools and typeclasses (such as actual functor and monad type classes).

like image 70
puhlen Avatar answered Sep 17 '22 23:09

puhlen


Scala has implicit ambient side-effects, just like C♯, Java, C++, Objective-C, Swift, C, Pascal, Basic, Perl, PHP, Python, Ruby, ECMAScript, etc. There is no type for IO.

There are third-party libraries that provide an IO type, but it doesn't give the same guarantees as Haskell's: Haskell has only a very few escape hatches (unsafePerformIO), in Scala, pretty much everything is an escape hatch.

However, there are ideas and I believe even research prototypes for effects systems in Scala, and the streamlined, more powerful, and sound type system of Dotty is probably going to be an even better foundation for that.

like image 35
Jörg W Mittag Avatar answered Sep 21 '22 23:09

Jörg W Mittag