scala - Specs 隐式转换与 Scala Predef 冲突

标签 scala testing types implicit-conversion specs

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

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 后,它似乎是 Specs 测试框架的一部分(我的测试是在 Specs 1 中编写的)。我尝试运行代码以获取解释器中的错误,并且在测试之外一切正常。

有什么方法可以消除歧义,以便将 Long 值传递给 Double/Time 函数?为什么 Specs 在 Scala 已经提供 LongToDouble 转换的情况下尝试创建它自己的转换?

最佳答案

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

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

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

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

  class MySpecification extends NoConversion {
     ...
  }

关于scala - Specs 隐式转换与 Scala Predef 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9050324/

相关文章:

javascript - Mocha 测试未使用 Meteor 运行 Chai 断言

matlab - 类型不完整的 Matlab 等式支持

testing - 如何使用 RequireJS 加载模块以在像 Jasmine 这样的测试框架中进行测试?

scala - 如何在 Scala 2.9.0 中覆盖 Iterable.flatMap?

scala - 如何从 Scala Play 中以 # 分隔的 url 中提取参数

scala - 与Clojure的线程宏等效的Scala是什么?

javascript - 在 Mac 上将 Behat 和 Mink 与 Selenium 和 Chrome 或 Safari 或 Firefox 连接

java - 返回与作为参数提供的相同类型 - Java8 泛型

.net - 有没有一种使用反射复制类型的直接方法?

scala - 使用 Play2/Scala 通过 Iteratee 将文件上传流转发到 S3