1

我正在学习 scalaZ 的 monad IO,但我无法理解 catchAll 和 catchSome 运算符的工作原理。我期待看到类似 RxJava 的 onError 或 onErrorrResumeNext 的行为,但没有捕获可抛出的对象,它只是破坏测试并抛出 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))

  }

并抓住一些例子

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

  }

知道我在做什么错吗?

问候

4

0 回答 0