规格隐式转换与Scala Predef冲突

3

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

type Time = Double

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

 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已经提供了这个功能呢?
2个回答

1

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

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

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

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

  class MySpecification extends NoConversion {
     ...
  }

0

尝试取消导入其中一个。

import NumericBaseMatchers.{longToDouble => _}

这似乎不起作用。NumericBaseMatchers是一个特质。导入时抱怨没有伴生对象? - seadowg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接