I've defined an implicit class providing a method foo
on all instances of Double
. Strangely, this method can now also be called e.g. on Float
instances, which is shown by the following example accepted by scalac
2.12.5 (use -Xscript Foo
):
implicit class DoubleOps(value: Double) {
def foo: Double = value
}
val x: Float = 1f
val y = x.foo
If I try to do the same with my own types, replacing Float
and Double
with MyFloat
and MyDouble
respectively, foo
is not available on MyFloat
instances.
trait MyDouble
object MyDouble {
implicit class MyFloatAsMyDouble(value: MyFloat) extends MyDouble
}
trait MyFloat
implicit class MyDoubleOps(value: MyDouble) {
def foo: MyDouble = value
}
val x: MyFloat = new MyFloat { }
val y = x.foo
$ scalac -Xscript Foo foo.scala
foo.scala:14: error: value foo is not a member of this.MyFloat
val y = x.foo
^
one error found
This is in line with my understand of how the compiler uses implicits to search for members not found on a type directly. But why does the first example still work?