zlaja
zlaja

Reputation: 1363

Scala implicit context resolution

I have found there rules for implicict resolution i SLS:

  1. if T is a compound type T1 with ... with Tn, the union of the parts of T1, ..., Tn, as well as T itself

  2. if T is a parameterized type S[T1, ..., Tn], the union of the parts of S and T1, ..., Tn

  3. if T is a singleton type p.type, the parts of the type of p

  4. if T is a type projection S#U, the parts of S as well as T itself

  5. in all other cases, just T itself

Is example below implicit resolution based on rule 4?

object Foo{
   trait Bar
   implicit def newBar = new Bar{
        override def toString = "Implicit Bar"
   }

}

implicitly[Foo.Bar]

Thanks

Zlaja

Upvotes: 1

Views: 385

Answers (1)

0__
0__

Reputation: 67280

Yes, I believe that is correct. I think for a singleton object Foo, type Foo.Bar is the same as Foo.type#Bar:

implicitly[Foo.type#Bar] // ok

Also:

def a(f: Foo.type#Bar) {}
def b(f: Foo.Bar) { a(f) }       // accepted

def c(f: Foo.Bar) {}
def d(f: Foo.type#Bar) { c(f) }  // accepted

Upvotes: 1

Related Questions