Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala implicit completing itself?

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).

like image 910
sthielo Avatar asked Oct 18 '22 10:10

sthielo


1 Answers

This is happening because f is declared as implicit. So in a way the right hand side of implicit val f: Foo.type = implicitly[Foo.type] resolves to the implicit val f itself !

If you remove the implicit from that line the compilation would fail. What i am wondering is why are you using such a line though.

This is definitely a issue to me. Someone looks like already took a note last year though.

Enhancement Request

like image 128
Som Bhattacharyya Avatar answered Oct 21 '22 05:10

Som Bhattacharyya