I used that kind of line to test my implicits making it implicit by copy-paste accident. It took me quite some time to figure out, why this compiles despite me not expecting it to compile:
> console
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66).
... skipped some comment lines ...
scala> case object Foo
defined object Foo
scala> object Bar { implicit val f: Foo.type = implicitly[Foo.type] }
defined object Bar
scala> val x = Bar.f
x: Foo.type = null
scala>
I would expect Bar to fail compilation, because there is NO implicit val of type Foo.type
(the case object is NOT declared implicit).
For me it looks like the compiler uses f's own declaration (left hand side) to complete its implementation (right hand side).
Is this really the intended behavior? At runtime this results in unexpected behavior with null
values (mostly NPEs for me).