diff options
author | Iain McGinniss <iainmcgin@gmail.com> | 2012-10-19 20:14:04 +0100 |
---|---|---|
committer | Iain McGinniss <iainmcgin@gmail.com> | 2012-10-19 20:14:04 +0100 |
commit | f938a7c834f4abe835048063306af6010a60c73c (patch) | |
tree | b073358771a0f4b10776043c511c29f0d4a43e47 | |
parent | 7c16776236dcf1d34551916dedefc22cfdc3f999 (diff) | |
download | scala-f938a7c834f4abe835048063306af6010a60c73c.tar.gz scala-f938a7c834f4abe835048063306af6010a60c73c.tar.bz2 scala-f938a7c834f4abe835048063306af6010a60c73c.zip |
Identifiers, Names and Scopes chapter converted. Minor CSS tweaks to
make examples and inline code look better.
-rw-r--r-- | 04-identifiers-names-and-scopes.md | 159 | ||||
-rwxr-xr-x | build.sh | 4 | ||||
-rw-r--r-- | resources/style.css | 13 |
3 files changed, 97 insertions, 79 deletions
diff --git a/04-identifiers-names-and-scopes.md b/04-identifiers-names-and-scopes.md index 2d0ba8b65f..6a7393db94 100644 --- a/04-identifiers-names-and-scopes.md +++ b/04-identifiers-names-and-scopes.md @@ -2,97 +2,102 @@ Identifiers, Names and Scopes ============================= Names in Scala identify types, values, methods, and classes which are -collectively called {\em entities}. Names are introduced by local -definitions and declarations (\sref{sec:defs}), inheritance (\sref{sec:members}), -import clauses (\sref{sec:import}), or package clauses -(\sref{sec:packagings}) which are collectively called {\em -bindings}. +collectively called _entities_. Names are introduced by local +[definitions and declarations](#basic-declarations-and-definitions), +[inheritance](#class-members), +[import clauses](#import-clauses), or +[package clauses](#packagings) +which are collectively called _bindings_. Bindings of different kinds have a precedence defined on them: -\begin{enumerate} -\item Definitions and declarations that are local, inherited, or made -available by a package clause in the same compilation unit where the -definition occurs have highest precedence. -\item Explicit imports have next highest precedence. -\item Wildcard imports have next highest precedence. -\item Definitions made available by a package clause not in the -compilation unit where the definition occurs have lowest precedence. -\end{enumerate} - -There are two different name spaces, one for types (\sref{sec:types}) -and one for terms (\sref{sec:exprs}). The same name may designate a + +#. Definitions and declarations that are local, inherited, or made + available by a package clause in the same compilation unit where the + definition occurs have highest precedence. +#. Explicit imports have next highest precedence. +#. Wildcard imports have next highest precedence. +#. Definitions made available by a package clause not in the + compilation unit where the definition occurs have lowest precedence. + + +There are two different name spaces, one for [types](#types) +and one for [terms](#expressions). The same name may designate a type and a term, depending on the context where the name is used. -A binding has a {\em scope} in which the entity defined by a single +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 {\em shadows} bindings of lower precedence in the +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. Note that shadowing is only a partial order. In a situation like -\begin{lstlisting} + +~~~~~~~~~~~~~~ {.scala} val x = 1; { import p.x; x } -\end{lstlisting} -neither binding of \code{x} shadows the other. Consequently, the -reference to \code{x} in the third line above would be ambiguous. +~~~~~~~~~~~~~~ + +neither binding of `x` shadows the other. Consequently, the +reference to `x` in the third line above would be ambiguous. -A reference to an unqualified (type- or term-) identifier $x$ is bound +A reference to an unqualified (type- or term-) identifier _x_ is bound by the unique binding, which -\begin{itemize} -\item defines an entity with name $x$ in the same namespace as the -identifier, and -\item shadows all other bindings that define entities with name $x$ in that namespace. -\end{itemize} -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 + +- defines an entity with name $x$ in the same namespace as the identifier, and +- shadows all other bindings that define entities with name _x_ in that + namespace. + +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. -\example Assume the following two definitions of a objects named \lstinline@X@ in packages \lstinline@P@ and \lstinline@Q@. -%ref: shadowing.scala -\begin{lstlisting} -package P { - object X { val x = 1; val y = 2 } -} -\end{lstlisting} -\begin{lstlisting} -package Q { - object X { val x = true; val y = "" } -} -\end{lstlisting} -The following program illustrates different kinds of bindings and -precedences between them. -\begin{lstlisting} -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 -}}}}}} -\end{lstlisting} - -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 -(\sref{sec:value-types}). 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`. + + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.scala} + 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. + + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.scala} + 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 + }}}}}} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +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](#value-types). The type of `e.x` is the member type of the +referenced entity in _T_. @@ -2,8 +2,8 @@ find . -name "*.md" | \ cat 01-title.md \ 02-preface.md \ - 03-lexical-syntax.md > build/ScalaReference.md -# 04-identifiers-names-and-scopes.md \ + 03-lexical-syntax.md \ + 04-identifiers-names-and-scopes.md > build/ScalaReference.md # 05-types.md \ # 06-basic-declarations-and-definitions.md \ # 07-classes-and-objects.md \ diff --git a/resources/style.css b/resources/style.css index b0230de748..9ef99126af 100644 --- a/resources/style.css +++ b/resources/style.css @@ -18,6 +18,7 @@ header .date { pre { margin-left: 3em; + margin-right: 3em; padding: 1em; background-color: #EEE; border: 1px solid #333; @@ -27,7 +28,19 @@ code { background-color: #EEE; } +code > span { + font-weight: normal !important; +} + +code { + padding-left: 0.1em; + padding-right: 0.1em; +} + +/* examples */ ol[type="1"] { + background-color: #E5ECF9; + border: 1px dashed black; list-style-type: none; } |