规范隐式转换与 Scala Predef 冲突
我的代码中有一个类型别名,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想停用继承的隐式转换,您可以这样做:
为了方便起见,如果您将其添加到新特征中,您可以在需要时将您的特征混合到您的规范中:
If you want to deactivate an inherited implicit conversion you can do this:
For convenience if you add it to a new trait, you can mix-in your trait to your specification when needed:
尝试取消导入其中之一。
Try unimporting one of them.