gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:00:30 27 4
gpt4 key购买 nike

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com