in the Scala , names are used to represent types, values, methods, and classes, which are called entities. Naming is present in local definitions and declarations, inheritance,import clauses, and package clauses, which can be collectively referred to as bindings.
binding has precedence, defined ( Partial or inherited ) has the highest priority, then the explicit Import , followed by a wildcard character Import , and then the package member, which is the lowest priority.
There are two different namespaces, one is the type and the other is the term. The same name can represent a type or term, which depends on the context in which the named app resides.
A binding has a domain that can be accessed with a simple name for an entity with a single named definition in this domain. Domains can be nested. Bindings in the internal domain will obscure the low-priority bindings in the same domain, or the lower-priority or same-priority bindings in the external domain.
Note that cloaking is just a partial order relationship. In the following scenario:
Val x = 1;
{ import p.x;
X
x the bindings do not cover each other. Therefore , the meaning of the reference to x in the third row will be ambiguous .
to an unqualified (type or term) identifier x references can be single-bound under the following conditions:
L Define an entity as an identifier in the same namespace as the name x
l obscures all other entity bindings that define the named X in this namespace
If there is no such binding will result in an error. If x by a import clause binding, then simply name x will be equivalent to import x bound by a definition or declaration, then x x
Example 2.0.2 The following is the package P and the Q the two are named X the definition of the object:
Package P {
Object x { val x = 1; val y = 2}
}
Package Q {
Object x { val x = true; val y = ""}
}
The following procedures indicate the different bindings and priorities between them.
Package P {//x is bound by the package clause
Import console._//println by a wildcard character Import binding
Object A {
println ("L4:" +x)// Here's X means p.x
Object B {
Import q._{//x is bound by a wildcard character import
println ("L7:" +x)// Here's X means q.x
Import x._//x and the y by a wildcard character Import binding
println ("L8:" +x)// Here's x means q.x.x
Object C {
Val x = 3//x bound by a local definition
println ("L12:" +x)// Here's x Reference Constants 3
{ importq.x._//x and the y by a wildcard character Import binding
//println ("L14:" +x)// here to x refers to ambiguous references
Import x.y//y by an explicit Import binding
println ("L16:" +y)// Here's y means q.x.y
{ valx = "abc"//x is bound by a local definition
Import p.x._//x and the y by a wildcard character Import binding
//println ("L19:" +y)// here to y refers to ambiguous references
println ("L20:" +x)// Here's x refers to a string "ABC"
}}}}}}
A to-qualified(type or term)identifierse.xrefers to a reference in the same namespaceeof typeTa name for thexmember as the identifier. IfTnot a value type will cause an error. e.xis the type of the referenced entityTthe type of the member.
Deep understanding of Scala identifiers, naming and domains