Scala implicit termination?

I used such a line to test my implications, which makes it implicit with copy-paste. It took me a while to figure out why this compiles, even though I did not expect 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 complete the compilation because there is no implicit val type Foo.type (the case object is NOT declared implicit).

It seems to me that the compiler uses its own declaration (left) to complete its implementation (right side).

Is this really intended behavior? At runtime, this leads to unexpected behavior with null values ​​(mostly for NPE for me).

+5
source share
1 answer

This is because f declared as implicit . So, in a sense, the right-hand side of implicit val f: Foo.type = implicitly[Foo.type] allows the implicit val f !

If you remove implicit from this line, compilation will fail. I wonder why you use such a line.

This is definitely a problem for me. Someone seems to have already taken a note last year.

Request for improvement

+3
source

All Articles