Monad IO catchAll运算符表现出来

时间:2018-04-22 17:36:43

标签: scala scalaz scalaz7

我正在学习scalaZ的monad IO,我无法理解catchAll和catchSome运算符是如何工作的。 我期待看到一个类似于RxJava的onError或onErrorrResumeNext的行为,但是没有抓住throwable,它只是打破了测试并抛出了NullPointerException ..

这是我的两个例子

  @Test
  def catchAllOperator(): Unit = {
    val errorSentence: IO[Throwable, String] =
      IO.point[Throwable, String](null)
        .map(value => value.toUpperCase())
        .catchAll(error => IO.fail(error))
    println(unsafePerformIO(errorSentence))

  }

和catchSome示例

  @Test
  def catchSomeOperator(): Unit = {
    val errorFunction = new PartialFunction[Throwable /*Entry type*/ , IO[Throwable, String] /*Output type*/ ] {

      override def isDefinedAt(x: Throwable): Boolean = x.isInstanceOf[NullPointerException]

      override def apply(v1: Throwable): IO[Throwable, String] = IO.point("Default value")
    }

    val errorSentence = IO.point[Throwable, String](null)
      .map(value => value.toUpperCase())
      .catchSome(errorFunction)
    println(unsafePerformIO(errorSentence))

  }

知道我做错了吗?

此致

0 个答案:

没有答案