Specs implicit conversion conflicts with Scala Predef

338 views Asked by At

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?

2

There are 2 answers

0
Eric On BEST ANSWER

If you want to deactivate an inherited implicit conversion you can do this:

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

For convenience if you add it to a new trait, you can mix-in your trait to your specification when needed:

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

  class MySpecification extends NoConversion {
     ...
  }
1
missingfaktor On

Try unimporting one of them.

import NumericBaseMatchers.{longToDouble => _}