I'm trying to learn Scalaz with a toy project of mine, I used monads in Haskell and now I want to learn how to use them in Scala with Scalaz.
The big question is, how does one use the IO() Monad in the Scala's main method?
In Haskell, the main function is of type IO() and in Scala it is of type ().
The solution I found so far was to create another function foo of type IO() and in the main method call foo.unsafePerformIO(). But this makes me cringe.
What could be a solution?
Scalaz provides a SafeApp trait that allows you to replace Scala's side-effectful main method with a wrapper that looks more like Haskell's main:
import scalaz._, Scalaz._, effect.{ IO, SafeApp }
object MyMain extends SafeApp {
override def runl(args: List[String]): IO[Unit] = IO(println("hello world"))
}
Now MyMain can be used like any other JVM class with a static main.
I don't personally use SafeApp much, but it's there if you want to avoid calling unsafePerformIO by hand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With