简体   繁体   中英

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.

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM