Names in Scala identify types, values, methods, and classes which are collectively called entities. Names are introduced by local definitions and declarations, inheritance, import clauses, or package clauses which are collectively called bindings.
Bindings of different kinds have a precedence defined on them:
There are two different name spaces, one for types and one for terms. The same name may designate a type and a term, depending on the context where the name is used.
A binding has a scope in which the entity defined by a single name can be accessed using a simple name. Scopes are nested. A binding in some inner scope shadows bindings of lower precedence in the same scope as well as bindings of the same or lower precedence in outer scopes.
A reference to an unqualified (type- or term-) identifier $x$ is bound by the unique binding, which
It is an error if no such binding exists. If $x$ is bound by an import clause, then the simple name $x$ is taken to be equivalent to the qualified name to which $x$ is mapped by the import clause. If $x$ is bound by a definition or declaration, then $x$ refers to the entity introduced by that binding. In that case, the type of $x$ is the type of the referenced entity.
A reference to a qualified (type- or term-) identifier $e.x$ refers to the member of the type $T$ of $e$ which has the name $x$ in the same namespace as the identifier. It is an error if $T$ is not a value type. The type of $e.x$ is the member type of the referenced entity in $T$.
Assume the following two definitions of a objects named X
in packages P
and Q
.
package P {
object X { val x = 1; val y = 2 }
}
package Q {
object X { val x = true; val y = "" }
}
The following program illustrates different kinds of bindings and precedences between them.
package P { // `X' bound by package clause
import Console._ // `println' bound by wildcard import
object A {
println("L4: "+X) // `X' refers to `P.X' here
object B {
import Q._ // `X' bound by wildcard import
println("L7: "+X) // `X' refers to `Q.X' here
import X._ // `x' and `y' bound by wildcard import
println("L8: "+x) // `x' refers to `Q.X.x' here
object C {
val x = 3 // `x' bound by local definition
println("L12: "+x) // `x' refers to constant `3' here
{ import Q.X._ // `x' and `y' bound by wildcard import
// println("L14: "+x) // reference to `x' is ambiguous here
import X.y // `y' bound by explicit import
println("L16: "+y) // `y' refers to `Q.X.y' here
{ val x = "abc" // `x' bound by local definition
import P.X._ // `x' and `y' bound by wildcard import
// println("L19: "+y) // reference to `y' is ambiguous here
println("L20: "+x) // `x' refers to string "abc" here
}}}}}}