|
| 1 | +--- |
| 2 | +title: Identifiers, Names & Scopes |
| 3 | +layout: default |
| 4 | +chapter: 2 |
| 5 | +--- |
| 6 | + |
| 7 | +# Identifiers, Names and Scopes |
| 8 | + |
| 9 | +Names in Scala identify types, values, methods, and classes which are |
| 10 | +collectively called _entities_. Names are introduced by local |
| 11 | +[definitions and declarations](04-basic-declarations-and-definitions.html#basic-declarations-and-definitions), |
| 12 | +[inheritance](05-classes-and-objects.html#class-members), |
| 13 | +[import clauses](04-basic-declarations-and-definitions.html#import-clauses), or |
| 14 | +[package clauses](09-top-level-definitions.html#packagings) |
| 15 | +which are collectively called _bindings_. |
| 16 | + |
| 17 | +Bindings of different kinds have precedence defined on them: |
| 18 | + |
| 19 | +1. Definitions and declarations that are local, inherited, or made |
| 20 | + available by a package clause and also defined in the same compilation unit |
| 21 | + as the reference to them, have the highest precedence. |
| 22 | +1. Explicit imports have the next highest precedence. |
| 23 | +1. Wildcard imports have the next highest precedence. |
| 24 | +1. Definitions made available by a package clause, but not also defined in the |
| 25 | + same compilation unit as the reference to them, as well as imports which |
| 26 | + are supplied by the compiler but not explicitly written in source code, |
| 27 | + have the lowest precedence. |
| 28 | + |
| 29 | +There are two different name spaces, one for [types](03-types.html#types) |
| 30 | +and one for [terms](06-expressions.html#expressions). The same name may designate a |
| 31 | +type and a term, depending on the context where the name is used. |
| 32 | + |
| 33 | +A binding has a _scope_ in which the entity defined by a single |
| 34 | +name can be accessed using a simple name. Scopes are nested. A binding |
| 35 | +in some inner scope _shadows_ bindings of lower precedence in the |
| 36 | +same scope as well as bindings of the same or lower precedence in outer |
| 37 | +scopes. |
| 38 | + |
| 39 | +Note that shadowing is only a partial order. In the following example, |
| 40 | +neither binding of `x` shadows the other. Consequently, the |
| 41 | +reference to `x` in the last line of the block is ambiguous. |
| 42 | + |
| 43 | +```scala |
| 44 | +val x = 1 |
| 45 | +locally { |
| 46 | + import p.X.x |
| 47 | + x |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +A reference to an unqualified (type- or term-) identifier ´x´ is bound |
| 52 | +by the unique binding, which |
| 53 | + |
| 54 | +- defines an entity with name ´x´ in the same namespace as the identifier, and |
| 55 | +- shadows all other bindings that define entities with name ´x´ in that |
| 56 | + namespace. |
| 57 | + |
| 58 | +It is an error if no such binding exists. If ´x´ is bound by an |
| 59 | +import clause, then the simple name ´x´ is taken to be equivalent to |
| 60 | +the qualified name to which ´x´ is mapped by the import clause. If ´x´ |
| 61 | +is bound by a definition or declaration, then ´x´ refers to the entity |
| 62 | +introduced by that binding. In that case, the type of ´x´ is the type |
| 63 | +of the referenced entity. |
| 64 | + |
| 65 | +A reference to a qualified (type- or term-) identifier ´e.x´ refers to |
| 66 | +the member of the type ´T´ of ´e´ which has the name ´x´ in the same |
| 67 | +namespace as the identifier. It is an error if ´T´ is not a [value type](03-types.html#value-types). |
| 68 | +The type of ´e.x´ is the member type of the referenced entity in ´T´. |
| 69 | + |
| 70 | +Binding precedence implies that the way source is bundled in files affects name resolution. |
| 71 | +In particular, imported names have higher precedence than names, defined in other files, |
| 72 | +that might otherwise be visible because they are defined in |
| 73 | +either the current package or an enclosing package. |
| 74 | + |
| 75 | +Note that a package definition is taken as lowest precedence, since packages |
| 76 | +are open and can be defined across arbitrary compilation units. |
| 77 | + |
| 78 | +```scala |
| 79 | +package util { |
| 80 | + import scala.util |
| 81 | + class Random |
| 82 | + object Test extends App { |
| 83 | + println(new util.Random) // scala.util.Random |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +The compiler supplies imports in a preamble to every source file. This preamble |
| 89 | +conceptually has the following form, where braces indicate nested scopes: |
| 90 | + |
| 91 | +```scala |
| 92 | +import java.lang._ |
| 93 | +{ |
| 94 | + import scala._ |
| 95 | + { |
| 96 | + import Predef._ |
| 97 | + { /* source */ } |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +These imports are taken as lowest precedence, so that they are always shadowed |
| 103 | +by user code, which may contain competing imports and definitions. |
| 104 | +They also increase the nesting depth as shown, so that later imports |
| 105 | +shadow earlier ones. |
| 106 | + |
| 107 | +As a convenience, multiple bindings of a type identifier to the same |
| 108 | +underlying type is permitted. This is possible when import clauses introduce |
| 109 | +a binding of a member type alias with the same binding precedence, typically |
| 110 | +through wildcard imports. This allows redundant type aliases to be imported |
| 111 | +without introducing an ambiguity. |
| 112 | + |
| 113 | +```scala |
| 114 | +object X { type T = annotation.tailrec } |
| 115 | +object Y { type T = annotation.tailrec } |
| 116 | +object Z { |
| 117 | + import X._, Y._, annotation.{tailrec => T} // OK, all T mean tailrec |
| 118 | + @T def f: Int = { f ; 42 } // error, f is not tail recursive |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +Similarly, imported aliases of names introduced by package statements are |
| 123 | +allowed, even though the names are strictly ambiguous: |
| 124 | + |
| 125 | +```scala |
| 126 | +// c.scala |
| 127 | +package p { class C } |
| 128 | + |
| 129 | +// xy.scala |
| 130 | +import p._ |
| 131 | +package p { class X extends C } |
| 132 | +package q { class Y extends C } |
| 133 | +``` |
| 134 | + |
| 135 | +The reference to `C` in the definition of `X` is strictly ambiguous |
| 136 | +because `C` is available by virtue of the package clause in |
| 137 | +a different file, and can't shadow the imported name. But because |
| 138 | +the references are the same, the definition is taken as though it |
| 139 | +did shadow the import. |
| 140 | + |
| 141 | +###### Example |
| 142 | + |
| 143 | +Assume the following two definitions of objects named `X` in packages `p` and `q` |
| 144 | +in separate compilation units. |
| 145 | + |
| 146 | +```scala |
| 147 | +package p { |
| 148 | + object X { val x = 1; val y = 2 } |
| 149 | +} |
| 150 | + |
| 151 | +package q { |
| 152 | + object X { val x = true; val y = false } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +The following program illustrates different kinds of bindings and |
| 157 | +precedences between them. |
| 158 | + |
| 159 | +```scala |
| 160 | +package p { // `X' bound by package clause |
| 161 | +import Console._ // `println' bound by wildcard import |
| 162 | +object Y { |
| 163 | + println(s"L4: $X") // `X' refers to `p.X' here |
| 164 | + locally { |
| 165 | + import q._ // `X' bound by wildcard import |
| 166 | + println(s"L7: $X") // `X' refers to `q.X' here |
| 167 | + import X._ // `x' and `y' bound by wildcard import |
| 168 | + println(s"L9: $x") // `x' refers to `q.X.x' here |
| 169 | + locally { |
| 170 | + val x = 3 // `x' bound by local definition |
| 171 | + println(s"L12: $x") // `x' refers to constant `3' here |
| 172 | + locally { |
| 173 | + import q.X._ // `x' and `y' bound by wildcard import |
| 174 | +// println(s"L15: $x") // reference to `x' is ambiguous here |
| 175 | + import X.y // `y' bound by explicit import |
| 176 | + println(s"L17: $y") // `y' refers to `q.X.y' here |
| 177 | + locally { |
| 178 | + val x = "abc" // `x' bound by local definition |
| 179 | + import p.X._ // `x' and `y' bound by wildcard import |
| 180 | +// println(s"L21: $y") // reference to `y' is ambiguous here |
| 181 | + println(s"L22: $x") // `x' refers to string "abc" here |
| 182 | +}}}}}} |
| 183 | +``` |
0 commit comments