Given this simple code snippet I am astounded to provoke a stack overflow this easy:
implicit def foobar: Unit = implicitly[Unit]
In my little more complex use case I have the following situtation:
abstract class Foo {
type Repr_Tpe
protected implicit def repr2Ordered: Repr_Tpe => Ordered[Repr_Tpe]
}
class Bar extends Foo {
type Repr_Tpe = Long
protected implicit def repr2Ordered = implicitly[Repr_Tpe => Ordered[Repr_Tpe]]
}
Defining method repr2Ordered
in class Foo
does not work because type Repr_Tpe
is abstract. So I decided to copy & paste the declaration and make a definition out of it; apparently leading to the stack overflow from above. Only by removing the modifier implicit
from the definition in class Bar
solves this problem.
Isn't there an elegant solution circumventing this pitfall?
You've defined
foobar
to be the implicit value of typeUnit
. Then you've defined it as the implicit value of typeUnit
. Thinking of it this way:You should be no more surprised that this causes a stack overflow than you would be by this statement:
For something with a bit more elegance, I would use a view bound to ensure that the type paramater is
Ordered
instead.