scala - 类型不匹配;带有 -Xstrict-inference 编译器选项的 result.type(具有底层类型 T)

标签 scala

我正在尝试使用 fold 方法丰富 scala.util.Try 类型。我有以下暗示要做到这一点:

implicit class FoldableTry[T](tryable: Try[T]) {
  def fold[X](failure: Throwable => X)(success: T => X): X = {
    tryable match {
      case Success(result) => success(result)
      case Failure(ex) => failure(ex)
    }
  }
}

当我使用 -Xstrict-inference 编译器选项运行 sbt compile 时,出现以下错误:

type mismatch;
[error]  found   : result.type (with underlying type T)
[error]  required: T
[error]         case Success(result) => success(result)
[error]                                         ^
[error] one error found
[error] (compile:compile) Compilation failed

如何解决这个错误?如果我删除它编译的编译器标志。

最佳答案

看起来您遇到了 bug (SI-6680) .我建议不要使用 -Xstrict-inference,因为它听起来很实验性——请注意 Paul Phillip 的评论:

-Xstrict-inference was intended only as a coarse hacky start, but it kind of coincided with my departure. I expect it is overflowing with implementation issues.

关于scala - 类型不匹配;带有 -Xstrict-inference 编译器选项的 result.type(具有底层类型 T),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31285475/

相关文章:

xml - 错误: The processing instruction target matching “[xX][mM][lL]” is not allowed

scala - 如何在 groupBy 之后聚合 map 列?

scala - 以ORC格式保存spark rdd

scala - Sbt 0.13 插件依赖和 scala-reflect.jar 版本冲突

scala - 如何从 Scala def 返回值

scala - 在 Scala 匿名函数中是否有更简洁的模式匹配方法?

string - 使用替换映射替换字符串上的多个单词

scala - akka actor 中的消息传递序列

scala - 为什么 Spark 项目中的 sbt 程序集失败并显示 "Please add any Spark dependencies by supplying the sparkVersion and sparkComponents"?

scala - 属性错误 : 'list' object has no attribute 'map'