I'm trying to write a Specs2 test for a class that takes a Squants Time as a parameter. The trick is that both tools define an implicit that adds a method called "seconds" to convert a number into their own representation (a squants.time.Seconds in one case and a org.specs2.time.Duration in the other), and unfortunately the wrong one seems to take precedence.
val a = new MyClass(10 seconds) // doesn't build because MyClass wants a Time instead of a Duration
To be clear, I could get around this by not relying on implicits to construct the Squants Time, that's not the question.
I rather like implicits so I decided to add an implicit to convert Durations into Times:
implicit def specsTimeToSquantsTime(t: org.specs2.time.Duration): squants.time.Time = squants.time.Seconds(t.toSeconds)
That got it to typecheck, but when I ran the test I got a stack overflow and the stack trace didn't make a great deal of sense, it said that my implicit conversion was calling itself (which wouldn't typecheck, and even if it would it still isn't possible given the above code!):
[error] package.TestConversions$.specsTimeToSquantsTime(TestConversions.scala:9)
[error] package.TestConversions$.specsTimeToSquantsTime(TestConversions.scala:9)
[error] package.TestConversions$.specsTimeToSquantsTime(TestConversions.scala:9)
...
So I have three questions: What's going on here? Is there a better way to do this? Could I manually hide the Int=>Duration implicit?
toSeconds
method in specs Duration, therefore implicit conversion in implicit conversion. – kiritsuku