Lexical scope

Let's start with the lexical scope. The lexical scope defines how variables are resolved in nested language constructs such as methods, functions, and other structured blocks. In general, the definitions of outer blocks are visible from inside the inner block (unless they are shadowed).

The following listing shows all possible conflicts during implicit resolution in this scope:


package object resolution {
implicit val a: TS = new TS("val in package object") // (1)
}

package resolution {
class TS(override val toString: String)
class Parent {
// implicit val c: TS = new TS("val in parent class") // (2)
}
trait Mixin {
// implicit val d: TS = new TS("val in mixin") // (3)
}
// import Outer._ // (4)
class Outer {
// implicit val e: TS = new TS("val in outer class") // (5)
// import Inner._ // (6)

class Inner(/*implicit (7) */ val arg: TS = implicitly[TS]) extends Parent with Mixin {
// implicit val f: TS = new TS("val in inner class") (8)
private val resolve = implicitly[TS]
}
object Inner {
implicit val g: TS = new TS("val in companion object")
}
}
object Outer {
implicit val h: TS = new TS("val in parent companion object")
}
}

The possible conflicts are underlined. It is easy to see that implicit values in the package object, outer and inner scope, as well as those brought into the inner or outer scope, are of the same weight. The parameter to the class constructor (7) will lead to the conflict as well, if it is declared implicit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.186.46