3

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

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 尝试创建自己的 LongToDouble 转换,而 Scala 已经提供了这种转换?

4

2 回答 2

1

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

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

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

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

  class MySpecification extends NoConversion {
     ...
  }
于 2012-01-29T20:21:49.920 回答
0

尝试取消导入其中之一。

import NumericBaseMatchers.{longToDouble => _}
于 2012-01-29T07:46:48.193 回答