首页 > 解决方案 > 视图边界已弃用;改用隐式参数

问题描述

我最近升级到 Scala 2.13,现在被警告弃用。我的功能如下所示:

implicit def convertGeneralResult[A <% ToResponseMarshallable, B <% ToResponseMarshallable](r: Either[A, B]) =
  r.fold[ToResponseMarshallable](identity, identity)

弃用警告显示(我实际上有两个,每个类型参数 A/B 一个):

视图边界已弃用;改用隐式参数。
示例:而不是def f[A <% Int](a: A)使用def f[A](a: A)(implicit ev: A => Int)

不过,我不完全确定如何在我的情况下应用建议的修复程序。

标签: scalaimplicitdeprecation-warningscala-2.13

解决方案


这个问题有很多深度,但我只会提供一些参考,以防你想深入研究它,以及如何解决它。

早在 2013 年,在 Scala 2.11 中,您就可以在 -Xfuture 下找到一个名为 deprecation warning for view bounds的 Scala 错误。在实现这一点时,但如果您没有使用SCALA COMPILER OPTIONS-Xfuture中的选项,您就不知道这一点。

在 2018 年,没有 -Xfuture 的弃用视图边界被打开,如您所见,它已合并到 Scala 2.13中。

作为另一个参考,有一个类似的问题Scala 2.13.0 deprecates <%, but how do I get rid of this in a class definition

现在到你的问题。您需要做的就是删除<%用法,并将其替换为隐式:

implicit def convertGeneralResult[A, B](r: Either[A, B])(implicit aToMarshallable: A => ToResponseMarshallable, bToMarshallable: B => ToResponseMarshallable) =
  r.fold[ToResponseMarshallable](identity, identity)

推荐阅读