规范隐式转换与 Scala Predef 冲突

发布于 2024-12-29 10:53:43 字数 859 浏览 0 评论 0原文

我的代码中有一个类型别名,如下所示:

type Time = Double

我经常在测试和应用程序中将 Long 值传递给使用此类型的函数。例如:

 def at(time : Time) : T = {
     // Do Something
 }

 at(System.currentTimeMillis)

此代码工作正常,除非在我的测试中运行时出现以下错误:

  found   : Long
  required: com.github.oetzi.echo.Echo.Time
  Note that implicit conversions are not applicable because they are ambiguous:
  both method long2double in object Predef of type (x: Long)Double
  and method longToDouble in trait NumericBaseMatchers of type (s: Long)Double
  are possible conversion functions from Long to com.github.oetzi.echo.Echo.Time

查找 NumericBaseMatchers 后,它似乎是规范测试框架的一部分(我的测试是在规范 1 中编写的)。我尝试运行代码来获取解释器中的错误,并且在测试之外一切正常。

有什么方法可以消除歧义,以便我可以将 Long 传递给 Double/Time 函数的值吗?当 Scala 已经提供了这种转换时,为什么 Specs 还要尝试创建自己的 LongToDouble 转换?

I have a type alias in my code like so:

type Time = Double

And I often in both tests and in applications pass Long values to functions that use this type. For instance:

 def at(time : Time) : T = {
     // Do Something
 }

 at(System.currentTimeMillis)

This code works fine unless run in my tests where I get the following error:

  found   : Long
  required: com.github.oetzi.echo.Echo.Time
  Note that implicit conversions are not applicable because they are ambiguous:
  both method long2double in object Predef of type (x: Long)Double
  and method longToDouble in trait NumericBaseMatchers of type (s: Long)Double
  are possible conversion functions from Long to com.github.oetzi.echo.Echo.Time

After looking up NumericBaseMatchers it seems its part of the Specs testing framework (my tests are written in Specs 1). I tried running code to get the error in the interpreter and it was fine out side of the tests.

Is there any way I can somehow remove the ambiguity so I can pass Long to values to a Double/Time function? Why does Specs try and create its own LongToDouble conversion when Scala already provides this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨扶苏 2025-01-05 10:53:43

如果您想停用继承的隐式转换,您可以这样做:

  override def longToDouble(s: Long) = super.longToDouble(s)

为了方便起见,如果您将其添加到新特征中,您可以在需要时将您的特征混合到您的规范中:

  trait NoConversion {
    override def longToDouble(s: Long) = super.longToDouble(s)
  }

  class MySpecification extends NoConversion {
     ...
  }

If you want to deactivate an inherited implicit conversion you can do this:

  override def longToDouble(s: Long) = super.longToDouble(s)

For convenience if you add it to a new trait, you can mix-in your trait to your specification when needed:

  trait NoConversion {
    override def longToDouble(s: Long) = super.longToDouble(s)
  }

  class MySpecification extends NoConversion {
     ...
  }
撩动你心 2025-01-05 10:53:43

尝试取消导入其中之一。

import NumericBaseMatchers.{longToDouble => _}

Try unimporting one of them.

import NumericBaseMatchers.{longToDouble => _}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文