3

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?

seadowg
  • 4,215
  • 6
  • 35
  • 43

2 Answers2

1

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 {
     ...
  }
Eric
  • 15,494
  • 38
  • 61
0

Try unimporting one of them.

import NumericBaseMatchers.{longToDouble => _}
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
  • This doesn't appear to work. NumericBaseMatchers is a trait. Importing complains that there is no companion object? – seadowg Jan 29 '12 at 18:07