-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should there be an IO Monad? #1224
Comments
I believe the omission of an IO or Task monad in Cats is on purpose. I've tried to list the reasons below:
|
Does it have something like an IO monad, then? Or is it just not a common pardigm used in Scala? If so, what is used to explicitly "mark" impure functions (although I know Scala is an impure language)? |
If I want what @adelbertc calls a pure IO Monad I simply alias a |
@CarlDong If stack safety is not a concern a plain encoding can be used: sealed abstract class IO[A] {
def unsafePerformIO(): A
def flatMap[B](f: A => IO[B]): IO[B] = f(unsafePerformIO())
def map[B](f: A => B): IO[B] =flatMap(a => IO.pure(f(a)))
}
object IO {
def apply[A](a: => A): IO[A] = new IO[A] { def unsafePerformIO(): A = a }
def pure[A](a: A): IO[A] = new IO[A] { def unsafePerformIO(): A = a }
} Short of that, for now you'll have to use something from an existing lib like FS2's Task or Doobie's IOLite: https://github.com/tpolecat/doobie/blob/series/0.3.x/yax/core/src/main/scala/doobie/util/iolite.scala |
I am more used to Haskell programming so I guess stack safety is pretty important... I will try, though. |
FYI I've created #1227 to add a note about |
@CarlDong If you're going to end up using streams, then bringing in something like scalaz-stream (aka FS2) (or Monix) is probably the way to go. If you also end up doing some database or HTTP work, there's also libraries like Doobie and HTTP4S that have bindings to scalaz-stream. |
I'm going to go ahead and close this out since there's now an item in the FAQ for this. Thanks for reminding me that it should be added @CarlDong. |
I noticed that Cats does not have an IO monad, while Scalaz does. Is there a reason not to include it in the current implementation? Or is it just "unimplemented"?
The text was updated successfully, but these errors were encountered: