【问题标题】:view bounds are deprecated; use an implicit parameter instead视图边界已弃用;改用隐式参数
【发布时间】:2021-04-10 21:06:59
【问题描述】:

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

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

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

视图边界已被弃用;请改用隐式参数。
例如:而不是def f[A &lt;% Int](a: A) 使用def f[A](a: A)(implicit ev: A =&gt; Int)

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

【问题讨论】:

  • 请注意,隐式转换 (这就是 view bounds 是什么) 被认为是一种不好的做法,这将是最好尝试编辑该代码以使其不依赖于此。

标签: scala implicit deprecation-warning scala-2.13


【解决方案1】:

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

早在 2013 年,您就可以在 Scala 2.11 中找到一个名为 deprecation warning for view bounds under -Xfuture 的 Scala 错误。当时this was implemented,但如果您没有在SCALA COMPILER OPTIONS 中使用-Xfuture 选项,您可能不会意识到这一点。

Deprecate view bounds without -Xfuture 于 2018 年开业,正如您所注意到的那样,was merged into Scala 2.13

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

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

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

【讨论】:

  • 哦,我明白了!我很困惑,因为我认为我实际上必须显式调用函数中的隐式参数才能将参数转换为折叠。但我不必这样做,只需声明隐式就足够了,这意味着必须存在一个隐式,它将类型从 A/B 带到ResponseMarshallable[A/B]。这是正确的解释吗?
  • 您可以参考thisthis的帖子了解更多信息。基本上这意味着A/B 可以转换为ToResponseMarshallable
  • 哦,是的,对不起。 ToResponseMarsallable 正如你所说(并且没有类型参数):)
猜你喜欢
  • 2013-07-20
  • 2011-10-13
  • 1970-01-01
  • 2021-01-06
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 2012-08-25
  • 2013-12-12
相关资源
最近更新 更多