diff --git a/spec/03-lexical-syntax.md b/spec/01-lexical-syntax.md similarity index 96% rename from spec/03-lexical-syntax.md rename to spec/01-lexical-syntax.md index c1b4d7f4ba80..64afd9e457b3 100644 --- a/spec/03-lexical-syntax.md +++ b/spec/01-lexical-syntax.md @@ -189,23 +189,23 @@ that a semicolon in place of the newline would be illegal in every one of these cases): - between the condition of a - [conditional expression](08-expressions.html#conditional-expressions) - or [while loop](08-expressions.html#while-loop-expressions) and the next + [conditional expression](06-expressions.html#conditional-expressions) + or [while loop](06-expressions.html#while-loop-expressions) and the next following expression, - between the enumerators of a - [for-comprehension](08-expressions.html#for-comprehensions-and-for-loops) + [for-comprehension](06-expressions.html#for-comprehensions-and-for-loops) and the next following expression, and - after the initial `type` keyword in a - [type definition or declaration](06-basic-declarations-and-definitions.html#type-declarations-and-type-aliases). + [type definition or declaration](04-basic-declarations-and-definitions.html#type-declarations-and-type-aliases). A single new line token is accepted - in front of an opening brace ‘{’, if that brace is a legal continuation of the current statement or expression, -- after an [infix operator](08-expressions.html#prefix-infix-and-postfix-operations), +- after an [infix operator](06-expressions.html#prefix-infix-and-postfix-operations), if the first token on the next line can start an expression, -- in front of a [parameter clause](06-basic-declarations-and-definitions.html#function-declarations-and-definitions), and -- after an [annotation](13-user-defined-annotations.html#user-defined-annotations). +- in front of a [parameter clause](04-basic-declarations-and-definitions.html#function-declarations-and-definitions), and +- after an [annotation](11-user-defined-annotations.html#user-defined-annotations). ### Example @@ -343,7 +343,7 @@ type `Long` are all integer numbers between $-2^{63}$ and $2^{63}-1$, inclusive. A compile-time error occurs if an integer literal denotes a number outside these ranges. -However, if the expected type [_pt_](08-expressions.html#expression-typing) of a literal +However, if the expected type [_pt_](06-expressions.html#expression-typing) of a literal in an expression is either `Byte`, `Short`, or `Char` and the integer number fits in the numeric range defined by the type, then the number is converted to type _pt_ and the literal's type @@ -506,7 +506,7 @@ lines. Method `stripMargin` is defined in class [scala.collection.immutable.StringLike](http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.StringLike). Because there is a predefined -[implicit conversion](08-expressions.html#implicit-conversions) from `String` to +[implicit conversion](06-expressions.html#implicit-conversions) from `String` to `StringLike`, the method is applicable to all strings. @@ -541,7 +541,7 @@ symbolLiteral ::= ‘'’ plainid ``` A symbol literal `'x` is a shorthand for the expression -`scala.Symbol("x")`. `Symbol` is a [case class](07-classes-and-objects.html#case-classes), +`scala.Symbol("x")`. `Symbol` is a [case class](05-classes-and-objects.html#case-classes), which is defined as follows. ```scala diff --git a/spec/01-title.md b/spec/01-title.md deleted file mode 100644 index c43908e61c9d..000000000000 --- a/spec/01-title.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -title: The Scala Language Specification -layout: default ---- - -# The Scala Language Specification, Version 2.11 - -## Martin Odersky, Philippe Altherr, Vincent Cremet, Gilles Dubochet, Burak Emir, Philipp Haller, Stéphane Micheloud, Adriaan Moors, Nikolay Mihaylov, Lukas Rytz, Michel Schinz, Erik Stenman, Matthias Zenger - - -## Markdown conversion by Iain McGinniss. - diff --git a/spec/04-identifiers-names-and-scopes.md b/spec/02-identifiers-names-and-scopes.md similarity index 90% rename from spec/04-identifiers-names-and-scopes.md rename to spec/02-identifiers-names-and-scopes.md index 0a3f9ca3cf84..bfb743dbe4a1 100644 --- a/spec/04-identifiers-names-and-scopes.md +++ b/spec/02-identifiers-names-and-scopes.md @@ -8,10 +8,10 @@ chapter: 2 Names in Scala identify types, values, methods, and classes which are collectively called _entities_. Names are introduced by local -[definitions and declarations](06-basic-declarations-and-definitions.html#basic-declarations-and-definitions), -[inheritance](07-classes-and-objects.html#class-members), -[import clauses](06-basic-declarations-and-definitions.html#import-clauses), or -[package clauses](11-top-level-definitions.html#packagings) +[definitions and declarations](04-basic-declarations-and-definitions.html#basic-declarations-and-definitions), +[inheritance](05-classes-and-objects.html#class-members), +[import clauses](04-basic-declarations-and-definitions.html#import-clauses), or +[package clauses](09-top-level-definitions.html#packagings) which are collectively called _bindings_. Bindings of different kinds have a precedence defined on them: @@ -25,8 +25,8 @@ Bindings of different kinds have a precedence defined on them: compilation unit where the definition occurs have lowest precedence. -There are two different name spaces, one for [types](05-types.html#types) -and one for [terms](08-expressions.html#expressions). The same name may designate a +There are two different name spaces, one for [types](03-types.html#types) +and one for [terms](06-expressions.html#expressions). 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 @@ -67,7 +67,7 @@ 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](05-types.html#value-types). +namespace as the identifier. It is an error if $T$ is not a [value type](03-types.html#value-types). The type of $e.x$ is the member type of the referenced entity in $T$. diff --git a/spec/02-preface.md b/spec/02-preface.md deleted file mode 100644 index 50e25d6c00d3..000000000000 --- a/spec/02-preface.md +++ /dev/null @@ -1,54 +0,0 @@ ---- -title: Preface -layout: default ---- - -## Preface - -Scala is a Java-like programming language which unifies -object-oriented and functional programming. It is a pure -object-oriented language in the sense that every value is an -object. Types and behavior of objects are described by -classes. Classes can be composed using mixin composition. Scala is -designed to work seamlessly with two less pure but mainstream -object-oriented languages -- Java and C#. - -Scala is a functional language in the sense that every function is a -value. Nesting of function definitions and higher-order functions are -naturally supported. Scala also supports a general notion of pattern -matching which can model the algebraic types used in many functional -languages. - -Scala has been designed to interoperate seamlessly with Java (an -alternative implementation of Scala also works for .NET). Scala -classes can call Java methods, create Java objects, inherit from Java -classes and implement Java interfaces. None of this requires interface -definitions or glue code. - -Scala has been developed from 2001 in the programming methods -laboratory at EPFL. Version 1.0 was released in November 2003. This -document describes the second version of the language, which was -released in March 2006. It acts a reference for the language -definition and some core library modules. It is not intended to teach -Scala or its concepts; for this there are other documents -[@scala-overview-tech-report; -@odersky:scala-experiment; -@odersky:sca; -@odersky-et-al:ecoop03; -@odersky-zenger:fool12] - -Scala has been a collective effort of many people. The design and the -implementation of version 1.0 was completed by Philippe Altherr, -Vincent Cremet, Gilles Dubochet, Burak Emir, Stéphane Micheloud, -Nikolay Mihaylov, Michel Schinz, Erik Stenman, Matthias Zenger, and -the author. Iulian Dragos, Gilles Dubochet, Philipp Haller, Sean -McDirmid, Lex Spoon, and Geoffrey Washburn joined in the effort to -develop the second version of the language and tools. Gilad Bracha, -Craig Chambers, Erik Ernst, Matthias Felleisen, Shriram Krishnamurti, -Gary Leavens, Sebastian Maneth, Erik Meijer, Klaus Ostermann, Didier -Rémy, Mads Torgersen, and Philip Wadler have shaped the design of -the language through lively and inspiring discussions and comments on -previous versions of this document. The contributors to the Scala -mailing list have also given very useful feedback that helped us -improve the language and its tools. - diff --git a/spec/05-types.md b/spec/03-types.md similarity index 97% rename from spec/05-types.md rename to spec/03-types.md index f614d15f8f03..c765a7681222 100644 --- a/spec/05-types.md +++ b/spec/03-types.md @@ -35,15 +35,15 @@ Value types are either _concrete_ or _abstract_. Every concrete value type can be represented as a _class type_, i.e. a [type designator](#type-designators) that refers to a -[class or a trait](07-classes-and-objects.html#class-definitions) [^1], or as a +[class or a trait](05-classes-and-objects.html#class-definitions) [^1], or as a [compound type](#compound-types) representing an intersection of types, possibly with a [refinement](#compound-types) that further constrains the types of its members. -Abstract value types are introduced by [type parameters](06-basic-declarations-and-definitions.html#type-parameters) -and [abstract type bindings](06-basic-declarations-and-definitions.html#type-declarations-and-type-aliases). +Abstract value types are introduced by [type parameters](04-basic-declarations-and-definitions.html#type-parameters) +and [abstract type bindings](04-basic-declarations-and-definitions.html#type-declarations-and-type-aliases). Parentheses in types can be used for grouping. [^1]: We assume that objects and packages also implicitly @@ -108,7 +108,7 @@ SimpleType ::= Path ‘.’ type ``` A singleton type is of the form `$p$.type`, where $p$ is a -path pointing to a value expected to [conform](08-expressions.html#expression-typing) +path pointing to a value expected to [conform](06-expressions.html#expression-typing) to `scala.AnyRef`. The type denotes the set of values consisting of `null` and the value denoted by $p$. @@ -259,7 +259,7 @@ AnnotType ::= SimpleType {Annotation} ``` An annotated type $T$ `$a_1 , \ldots , a_n$` -attaches [annotations](13-user-defined-annotations.html#user-defined-annotations) +attaches [annotations](11-user-defined-annotations.html#user-defined-annotations) $a_1 , \ldots , a_n$ to the type $T$. ### Example @@ -288,7 +288,7 @@ $T_1 , \ldots , T_n$ and the refinement `{ $R$ }`. A refinement `{ $R$ }` contains declarations and type definitions. If a declaration or definition overrides a declaration or definition in one of the component types $T_1 , \ldots , T_n$, the usual rules for -[overriding](07-classes-and-objects.html#overriding) apply; otherwise the declaration +[overriding](05-classes-and-objects.html#overriding) apply; otherwise the declaration or definition is said to be “structural” [^2]. [^2]: A reference to a structurally defined member (method call or access @@ -355,10 +355,10 @@ $T_2$. The type is equivalent to the type application `$\mathit{op}$[$T_1$, $T_2$]`. The infix operator $\mathit{op}$ may be an arbitrary identifier, except for `*`, which is reserved as a postfix modifier -denoting a [repeated parameter type](06-basic-declarations-and-definitions.html#repeated-parameters). +denoting a [repeated parameter type](04-basic-declarations-and-definitions.html#repeated-parameters). All type infix operators have the same precedence; parentheses have to -be used for grouping. The [associativity](08-expressions.html#prefix-infix-and-postfix-operations) +be used for grouping. The [associativity](06-expressions.html#prefix-infix-and-postfix-operations) of a type operator is determined as for term operators: type operators ending in a colon ‘:’ are right-associative; all other operators are left-associative. @@ -385,7 +385,7 @@ values that take arguments of types $T1 , \ldots , Tn$ and yield results of type $U$. In the case of exactly one argument type $T \Rightarrow U$ is a shorthand for $(T) \Rightarrow U$. An argument type of the form $\Rightarrow T$ -represents a [call-by-name parameter](06-basic-declarations-and-definitions.html#by-name-parameters) of type $T$. +represents a [call-by-name parameter](04-basic-declarations-and-definitions.html#by-name-parameters) of type $T$. Function types associate to the right, e.g. $S \Rightarrow T \Rightarrow U$ is the same as @@ -405,7 +405,7 @@ trait Function_n[-T1 , … , -T$_n$, +R] { } ``` -Hence, function types are [covariant](06-basic-declarations-and-definitions.html#variance-annotations) in their +Hence, function types are [covariant](04-basic-declarations-and-definitions.html#variance-annotations) in their result type and contravariant in their argument types. ### Existential Types @@ -420,7 +420,7 @@ ExistentialDcl ::= ‘type’ TypeDcl An existential type has the form `$T$ forSome { $Q$ }` where $Q$ is a sequence of -[type declarations](06-basic-declarations-and-definitions.html#type-declarations-and-type-aliases). +[type declarations](04-basic-declarations-and-definitions.html#type-declarations-and-type-aliases). Let $t_1[\mathit{tps}_1] >: L_1 <: U_1 , \ldots , t_n[\mathit{tps}_n] >: L_n <: U_n$ @@ -462,7 +462,7 @@ is equivalent to 1. An existential type `$T$ forSome { $Q$ }` where $Q$ contains a clause `type $t[\mathit{tps}] >: L <: U$` is equivalent to the type `$T'$ forSome { $Q$ }` where $T'$ results from $T$ by replacing -every [covariant occurrence](06-basic-declarations-and-definitions.html#variance-annotations) of $t$ in $T$ by $U$ and by +every [covariant occurrence](04-basic-declarations-and-definitions.html#variance-annotations) of $t$ in $T$ by $U$ and by replacing every contravariant occurrence of $t$ in $T$ by $L$. @@ -591,7 +591,7 @@ that are re-evaluated each time the parameterless method name is referenced. Method types do not exist as types of values. If a method name is used -as a value, its type is [implicitly converted](08-expressions.html#implicit-conversions) to a +as a value, its type is [implicitly converted](06-expressions.html#implicit-conversions) to a corresponding function type. ###### Example @@ -645,8 +645,8 @@ union : [A >: Nothing <: Comparable[A]] (x: Set[A], xs: Set[A]) Set[A] . A type constructor is represented internally much like a polymorphic method type. `[$\pm$ $a_1$ >: $L_1$ <: $U_1 , \ldots , \pm a_n$ >: $L_n$ <: $U_n$] $T$` represents a type that is expected by a -[type constructor parameter](06-basic-declarations-and-definitions.html#type-parameters) or an -[abstract type constructor binding](06-basic-declarations-and-definitions.html#type-declarations-and-type-aliases) with +[type constructor parameter](04-basic-declarations-and-definitions.html#type-parameters) or an +[abstract type constructor binding](04-basic-declarations-and-definitions.html#type-declarations-and-type-aliases) with the corresponding type parameter clause. ###### Example diff --git a/spec/06-basic-declarations-and-definitions.md b/spec/04-basic-declarations-and-definitions.md similarity index 97% rename from spec/06-basic-declarations-and-definitions.md rename to spec/04-basic-declarations-and-definitions.md index e0fdf051c24e..ab1f98ea0793 100644 --- a/spec/06-basic-declarations-and-definitions.md +++ b/spec/04-basic-declarations-and-definitions.md @@ -21,8 +21,8 @@ Def ::= PatVarDef ``` A _declaration_ introduces names and assigns them types. It can -form part of a [class definition](07-classes-and-objects.html#templates) or of a -refinement in a [compound type](05-types.html#compound-types). +form part of a [class definition](05-classes-and-objects.html#templates) or of a +refinement in a [compound type](03-types.html#compound-types). A _definition_ introduces names that denote terms or types. It can form part of an object or class definition or it can be local to a @@ -116,7 +116,7 @@ type $T$. A value definition `val $x$: $T$ = $e$` defines $x$ as a name of the value that results from the evaluation of $e$. If the value definition is not recursive, the type -$T$ may be omitted, in which case the [packed type](08-expressions.html#expression-typing) of +$T$ may be omitted, in which case the [packed type](06-expressions.html#expression-typing) of expression $e$ is assumed. If a type $T$ is given, then $e$ is expected to conform to it. @@ -132,13 +132,13 @@ A _constant value definition_ is of the form final val x = e ``` -where `e` is a [constant expression](08-expressions.html#constant-expressions). +where `e` is a [constant expression](06-expressions.html#constant-expressions). The `final` modifier must be present and no type annotation may be given. References to the constant value `x` are themselves treated as constant expressions; in the generated code they are replaced by the definition's right-hand side `e`. -Value definitions can alternatively have a [pattern](10-pattern-matching.html#patterns) +Value definitions can alternatively have a [pattern](08-pattern-matching.html#patterns) as left-hand side. If $p$ is some pattern other than a simple name or a name followed by a colon and a type, then the value definition `val $p$ = $e$` is expanded as follows: @@ -223,9 +223,9 @@ A variable definition `var $x$: $T$ = $e$` introduces a mutable variable with type $T$ and initial value as given by the expression $e$. The type $T$ can be omitted, in which case the type of $e$ is assumed. If $T$ is given, then $e$ is expected to -[conform to it](08-expressions.html#expression-typing). +[conform to it](06-expressions.html#expression-typing). -Variable definitions can alternatively have a [pattern](10-pattern-matching.html#patterns) +Variable definitions can alternatively have a [pattern](08-pattern-matching.html#patterns) as left-hand side. A variable definition `var $p$ = $e$` where $p$ is a pattern other than a simple name or a name followed by a colon and a type is expanded in the same way @@ -326,7 +326,7 @@ the top type `scala.Any` is assumed. A type constructor declaration imposes additional restrictions on the concrete types for which $t$ may stand. Besides the bounds $L$ and $U$, the type parameter clause may impose higher-order bounds and -variances, as governed by the [conformance of type constructors](05-types.html#conformance). +variances, as governed by the [conformance of type constructors](03-types.html#conformance). The scope of a type parameter extends over the bounds `>: $L$ <: $U$` and the type parameter clause $\mathit{tps}$ itself. A higher-order type parameter clause (of an abstract type constructor @@ -414,7 +414,7 @@ function definitions. In this section we consider only type parameter definitions with lower bounds `>: $L$` and upper bounds `<: $U$` whereas a discussion of context bounds `: $U$` and view bounds `<% $U$` -is deferred to [here](09-implicit-parameters-and-views.html#context-bounds-and-view-bounds). +is deferred to [here](07-implicit-parameters-and-views.html#context-bounds-and-view-bounds). The most general form of a first-order type parameter is `$@a_1 \ldots @a_n$ $\pm$ $t$ >: $L$ <: $U$`. @@ -468,7 +468,7 @@ The following type parameter clauses are illegal: ## Variance Annotations Variance annotations indicate how instances of parameterized types -vary with respect to [subtyping](05-types.html#conformance). A +vary with respect to [subtyping](03-types.html#conformance). A ‘+’ variance indicates a covariant dependency, a ‘-’ variance indicates a contravariant dependency, and a missing variance indication indicates an invariant dependency. @@ -511,7 +511,7 @@ changes at the following constructs. References to the type parameters in -[object-private or object-protected values, types, variables, or methods](07-classes-and-objects.html#modifiers) of the class are not +[object-private or object-protected values, types, variables, or methods](05-classes-and-objects.html#modifiers) of the class are not checked for their variance position. In these members the type parameter may appear anywhere without restricting its legal variance annotations. @@ -618,7 +618,7 @@ followed by zero or more value parameter clauses introduces a value with a (possibly polymorphic) method type whose parameter types and result type are as given. -The type of the function body is expected to [conform](08-expressions.html#expression-typing) +The type of the function body is expected to [conform](06-expressions.html#expression-typing) to the function's declared result type, if one is given. If the function definition is not recursive, the result type may be omitted, in which case it is @@ -688,7 +688,7 @@ The by-name modifier is disallowed for parameters of classes that carry a `val` or `var` prefix, including parameters of case classes for which a `val` prefix is implicitly generated. The by-name modifier is also disallowed for -[implicit parameters](09-implicit-parameters-and-views.html#implicit-parameters). +[implicit parameters](07-implicit-parameters-and-views.html#implicit-parameters). ###### Example The declaration @@ -868,10 +868,10 @@ ImportSelector ::= id [‘=>’ id | ‘=>’ ‘_’] ``` An import clause has the form `import $p$.$I$` where $p$ is a -[stable identifier](05-types.html#paths) and $I$ is an import expression. +[stable identifier](03-types.html#paths) and $I$ is an import expression. The import expression determines a set of names of importable members of $p$ which are made available without qualification. A member $m$ of $p$ is -_importable_ if it is not [object-private](07-classes-and-objects.html#modifiers). +_importable_ if it is not [object-private](05-classes-and-objects.html#modifiers). The most general form of an import expression is a list of _import selectors_ ```scala diff --git a/spec/07-classes-and-objects.md b/spec/05-classes-and-objects.md similarity index 97% rename from spec/07-classes-and-objects.md rename to spec/05-classes-and-objects.md index 08528037672e..70fa3e0272f6 100644 --- a/spec/07-classes-and-objects.md +++ b/spec/05-classes-and-objects.md @@ -60,7 +60,7 @@ chain in the inheritance hierarchy which starts with the template's superclass. The _least proper supertype_ of a template is the class type or -[compound type](05-types.html#compound-types) consisting of all its parent +[compound type](03-types.html#compound-types) consisting of all its parent class types. The statement sequence $\mathit{stats}$ contains member definitions that @@ -157,10 +157,10 @@ objects created by an instance creation expression, or of parts of an object's definition which are inherited by a class or object definition. A constructor invocation is a function application `$x$.$c$[$\mathit{targs}$]($\mathit{args}_1$)$\ldots$($\mathit{args}_n$)`, where $x$ is a -[stable identifier](05-types.html#paths), $c$ is a type name which either designates a +[stable identifier](03-types.html#paths), $c$ is a type name which either designates a class or defines an alias type for one, $\mathit{targs}$ is a type argument list, $\mathit{args}_1 , \ldots , \mathit{args}_n$ are argument lists, and there is a -constructor of that class which is [applicable](08-expressions.html#function-applications) +constructor of that class which is [applicable](06-expressions.html#function-applications) to the given arguments. If the constructor invocation uses named or default arguments, it is transformed into a block expression using the same transformation as described [here](sec:named-default). @@ -168,7 +168,7 @@ same transformation as described [here](sec:named-default). The prefix `$x$.` can be omitted. A type argument list can be given only if the class $c$ takes type parameters. Even then it can be omitted, in which case a type argument list is synthesized -using [local type inference](08-expressions.html#local-type-inference). If no explicit +using [local type inference](06-expressions.html#local-type-inference). If no explicit arguments are given, an empty list `()` is implicitly supplied. An evaluation of a constructor invocation @@ -302,7 +302,7 @@ linearization of $C$) the class in which $M'$ is defined. It is an error if a template directly defines two matching members. It is also an error if a template contains two members (directly defined -or inherited) with the same name and the same [erased type](05-types.html#type-erasure). +or inherited) with the same name and the same [erased type](03-types.html#type-erasure). Finally, a template is not allowed to contain two methods (directly defined or inherited) with the same name which both define default arguments. @@ -329,7 +329,7 @@ trait `B`. A member $M$ of class $C$ that [matches](#class-members) a non-private member $M'$ of a base class of $C$ is said to _override_ that member. In this case -the binding of the overriding member $M$ must [subsume](05-types.html#conformance) +the binding of the overriding member $M$ must [subsume](03-types.html#conformance) the binding of the overridden member $M'$. Furthermore, the following restrictions on modifiers apply to $M$ and $M'$: @@ -361,7 +361,7 @@ class Y extends X { override var stable = 1 } // error ``` Another restriction applies to abstract type members: An abstract type -member with a [volatile type](05-types.html#volatile-types) as its upper +member with a [volatile type](03-types.html#volatile-types) as its upper bound may not override an abstract type member which does not have a volatile upper bound. @@ -594,7 +594,7 @@ it is _concrete_ if a full definition is given. The `abstract` modifier is used in class definitions. It is redundant for traits, and mandatory for all other classes which have incomplete members. Abstract classes cannot be -[instantiated](08-expressions.html#instance-creation-expressions) with a constructor invocation +[instantiated](06-expressions.html#instance-creation-expressions) with a constructor invocation unless followed by mixins and/or a refinement which override all incomplete members of the class. Only abstract classes and traits can have abstract term members. @@ -610,7 +610,7 @@ overridden in subclasses. A `final` class may not be inherited by a template. `final` is redundant for object definitions. Members of final classes or objects are implicitly also final, so the `final` modifier is generally redundant for them, too. Note, however, that -[constant value definitions](06-basic-declarations-and-definitions.html#value-declarations-and-definitions) do require +[constant value definitions](04-basic-declarations-and-definitions.html#value-declarations-and-definitions) do require an explicit `final` modifier, even if they are defined in a final class or object. `final` may not be applied to incomplete members, and it may not be combined in one modifier list with `sealed`. @@ -713,7 +713,7 @@ Here, parameter section is called _polymorphic_, otherwise it is called _monomorphic_. - $as$ is a possibly empty sequence of - [annotations](13-user-defined-annotations.html#user-defined-annotations). + [annotations](11-user-defined-annotations.html#user-defined-annotations). If any annotations are given, they apply to the primary constructor of the class. - $m$ is an [access modifier](#modifiers) such as @@ -728,16 +728,16 @@ Here, If no formal parameter sections are given, an empty parameter section `()` is assumed. If a formal parameter declaration $x: T$ is preceded by a `val` - or `var` keyword, an accessor (getter) [definition](06-basic-declarations-and-definitions.html#variable-declarations-and-definitions) + or `var` keyword, an accessor (getter) [definition](04-basic-declarations-and-definitions.html#variable-declarations-and-definitions) for this parameter is implicitly added to the class. The getter introduces a value member $x$ of class $c$ that is defined as an alias of the parameter. - If the introducing keyword is `var`, a setter accessor [`$x$_=`](06-basic-declarations-and-definitions.html#variable-declarations-and-definitions) is also implicitly added to the class. + If the introducing keyword is `var`, a setter accessor [`$x$_=`](04-basic-declarations-and-definitions.html#variable-declarations-and-definitions) is also implicitly added to the class. In invocation of that setter `$x$_=($e$)` changes the value of the parameter to the result of evaluating $e$. The formal parameter declaration may contain modifiers, which then carry over to the accessor definition(s). When access modifiers are given for a parameter, but no `val` or `var` keyword, `val` is assumed. - A formal parameter prefixed by `val` or `var` may not at the same time be a [call-by-name parameter](06-basic-declarations-and-definitions.html#by-name-parameters). + A formal parameter prefixed by `val` or `var` may not at the same time be a [call-by-name parameter](04-basic-declarations-and-definitions.html#by-name-parameters). - $t$ is a [template](#templates) of the form @@ -823,7 +823,7 @@ If there are auxiliary constructors of a class $C$, they form together with $C$'s primary [constructor](#class-definitions) an overloaded constructor definition. The usual rules for -[overloading resolution](08-expressions.html#overloading-resolution) +[overloading resolution](06-expressions.html#overloading-resolution) apply for constructor invocations of $C$, including for the self constructor invocations in the constructor expressions themselves. However, unlike other methods, constructors @@ -871,7 +871,7 @@ definition for the parameter is [generated](#class-definitions). A case class definition of `$c$[$\mathit{tps}\,$]($\mathit{ps}_1\,$)$\ldots$($\mathit{ps}_n$)` with type parameters $\mathit{tps}$ and value parameters $\mathit{ps}$ implicitly -generates an [extractor object](10-pattern-matching.html#extractor-patterns) which is +generates an [extractor object](08-pattern-matching.html#extractor-patterns) which is defined as follows: ```scala @@ -905,7 +905,7 @@ def unapply[$\mathit{tps}\,$]($x$: $c$[$\mathit{tps}\,$]) = x ne null The name of the `unapply` method is changed to `unapplySeq` if the first parameter section $\mathit{ps}_1$ of $c$ ends in a -[repeated parameter](06-basic-declarations-and-definitions.html#repeated-parameters). +[repeated parameter](04-basic-declarations-and-definitions.html#repeated-parameters). If a companion object $c$ exists already, no new object is created, but the `apply` and `unapply` methods are added to the existing object instead. @@ -926,7 +926,7 @@ In all cases `$x_{i,j}$` and `$T_{i,j}$` refer to the name and type of the corre `$\mathit{ps}_{i,j}$`. Every case class implicitly overrides some method definitions of class -[`scala.AnyRef`](14-the-scala-standard-library.html#root-classes) unless a definition of the same +[`scala.AnyRef`](12-the-scala-standard-library.html#root-classes) unless a definition of the same method is already given in the case class itself or a concrete definition of the same method is given in some base class of the case class different from `AnyRef`. In particular: @@ -1002,7 +1002,7 @@ initialized after the superclass is initialized. Assume a trait $D$ defines some aspect of an instance $x$ of type $C$ (i.e. $D$ is a base class of $C$). Then the _actual supertype_ of $D$ in $x$ is the compound type consisting of all the base classes in $\mathcal{L}(C)$ that succeed $D$. The actual supertype gives -the context for resolving a [`super` reference](08-expressions.html#this-and-super) in a trait. +the context for resolving a [`super` reference](06-expressions.html#this-and-super) in a trait. Note that the actual supertype depends on the type to which the trait is added in a mixin composition; it is not statically known at the time the trait is defined. @@ -1133,7 +1133,7 @@ constructor is being evaluated block until evaluation is complete. The expansion given above is not accurate for top-level objects. It cannot be because variable and method definition cannot appear on the -top-level outside of a [package object](11-top-level-definitions.html#package-objects). Instead, +top-level outside of a [package object](09-top-level-definitions.html#package-objects). Instead, top-level objects are translated to static fields. ###### Example diff --git a/spec/08-expressions.md b/spec/06-expressions.md similarity index 96% rename from spec/08-expressions.md rename to spec/06-expressions.md index db1bd182cd5a..9a558420dd24 100644 --- a/spec/08-expressions.md +++ b/spec/06-expressions.md @@ -59,7 +59,7 @@ The typing of expressions is often relative to some _expected type_ (which migh The following skolemization rule is applied universally for every expression: If the type of an expression would be an existential type $T$, then the type of the expression is assumed instead to be a -[skolemization](05-types.html#existential-types) of $T$. +[skolemization](03-types.html#existential-types) of $T$. Skolemization is reversed by type packing. Assume an expression $e$ of type $T$ and let $t_1[\mathit{tps}_1] >: L_1 <: U_1 , \ldots , t_n[\mathit{tps}_n] >: L_n <: U_n$ be @@ -77,7 +77,7 @@ $T$ forSome { type $t_1[\mathit{tps}_1] >: L_1 <: U_1$; $\ldots$; type $t_n[\mat SimpleExpr ::= Literal ``` -Typing of literals is as described [here](03-lexical-syntax.html#literals); their +Typing of literals is as described [here](01-lexical-syntax.html#literals); their evaluation is immediate. @@ -93,7 +93,7 @@ implements methods in class `scala.AnyRef` as follows: - `ne($x\,$)` and `!=($x\,$)` return true iff the argument x is not also the "null" object. - `isInstanceOf[$T\,$]` always returns `false`. -- `asInstanceOf[$T\,$]` returns the [default value](06-basic-declarations-and-definitions.html#value-declarations-and-definitions) of type $T$. +- `asInstanceOf[$T\,$]` returns the [default value](04-basic-declarations-and-definitions.html#value-declarations-and-definitions) of type $T$. - `##` returns ``0``. A reference to any other member of the "null" object causes a @@ -111,14 +111,14 @@ A designator refers to a named term. It can be a _simple name_ or a _selection_. A simple name $x$ refers to a value as specified -[here](04-identifiers-names-and-scopes.html#identifiers-names-and-scopes). +[here](02-identifiers-names-and-scopes.html#identifiers-names-and-scopes). If $x$ is bound by a definition or declaration in an enclosing class or object $C$, it is taken to be equivalent to the selection `$C$.this.$x$` where $C$ is taken to refer to the class containing $x$ -even if the type name $C$ is [shadowed](04-identifiers-names-and-scopes.html#identifiers-names-and-scopes) at the +even if the type name $C$ is [shadowed](02-identifiers-names-and-scopes.html#identifiers-names-and-scopes) at the occurrence of $x$. -If $r$ is a [stable identifier](05-types.html#paths) of type $T$, the selection $r.x$ refers +If $r$ is a [stable identifier](03-types.html#paths) of type $T$, the selection $r.x$ refers statically to a term member $m$ of $r$ that is identified in $T$ by the name $x$. @@ -132,8 +132,8 @@ $y$. The expected type of a designator's prefix is always undefined. The type of a designator is the type $T$ of the entity it refers to, with -the following exception: The type of a [path](05-types.html#paths) $p$ -which occurs in a context where a [stable type](05-types.html#singleton-types) +the following exception: The type of a [path](03-types.html#paths) $p$ +which occurs in a context where a [stable type](03-types.html#singleton-types) is required is the singleton type `$p$.type`. The contexts where a stable type is required are those that satisfy @@ -308,7 +308,7 @@ and the argument passed to the function is `$y_i$()`. The last argument in an application may be marked as a sequence argument, e.g. `$e$: _*`. Such an argument must correspond -to a [repeated parameter](06-basic-declarations-and-definitions.html#repeated-parameters) of type +to a [repeated parameter](04-basic-declarations-and-definitions.html#repeated-parameters) of type `$S$*` and it must be the only argument matching this parameter (i.e. the number of formal parameters and actual arguments must be the same). Furthermore, the type of $e$ must conform to @@ -396,7 +396,7 @@ positional arguments and to $e'_i$ for named arguments of the form `$x_i=e'_i$`. Then, for every parameter which is not specified by the argument list, a value definition using a fresh name $z_i$ is created, which is initialized using the method computing the -[default argument](06-basic-declarations-and-definitions.html#function-declarations-and-definitions) of +[default argument](04-basic-declarations-and-definitions.html#function-declarations-and-definitions) of this parameter. Let $\mathit{args}$ be a permutation of the generated names $y_i$ and $z_i$ such such @@ -501,11 +501,11 @@ SimpleExpr ::= `new' (ClassTemplate | TemplateBody) A simple instance creation expression is of the form `new $c$` -where $c$ is a [constructor invocation](07-classes-and-objects.html#constructor-invocations). Let $T$ be +where $c$ is a [constructor invocation](05-classes-and-objects.html#constructor-invocations). Let $T$ be the type of $c$. Then $T$ must denote a (a type instance of) a non-abstract subclass of `scala.AnyRef`. Furthermore, the _concrete self type_ of the -expression must conform to the [self type](07-classes-and-objects.html#templates) of the class denoted by +expression must conform to the [self type](05-classes-and-objects.html#templates) of the class denoted by $T$. The concrete self type is normally $T$, except if the expression `new $c$` appears as the right hand side of a value definition @@ -523,7 +523,7 @@ object of type $T$ which is is initialized by evaluating $c$. The type of the expression is $T$. A general instance creation expression is of the form -`new $t$` for some [class template](07-classes-and-objects.html#templates) $t$. +`new $t$` for some [class template](05-classes-and-objects.html#templates) $t$. Such an expression is equivalent to the block ```scala @@ -582,7 +582,7 @@ undefined. The type of a block `$s_1$; $\ldots$; $s_n$; $e$` is `$T$ forSome {$\,Q\,$}`, where $T$ is the type of $e$ and $Q$ -contains [existential clauses](05-types.html#existential-types) +contains [existential clauses](03-types.html#existential-types) for every value or type name which is free in $T$ and which is defined locally in one of the statements $s_1 , \ldots , s_n$. We say the existential clause _binds_ the occurrence of the value or type name. @@ -620,7 +620,7 @@ The block { class C extends B {$\ldots$} ; new C } ``` -simply has type `B`, because with the rules [here](05-types.html#simplification-rules) +simply has type `B`, because with the rules [here](03-types.html#simplification-rules) the existentially quantified type `_1 forSome { type _1 <: B }` can be simplified to `B`. @@ -727,7 +727,7 @@ name. ### Assignment Operators An assignment operator is an operator symbol (syntax category -`op` in [Identifiers](03-lexical-syntax.html#identifiers)) that ends in an equals character +`op` in [Identifiers](01-lexical-syntax.html#identifiers)) that ends in an equals character “`=`”, with the exception of operators for which one of the following conditions holds: @@ -787,7 +787,7 @@ Expr1 ::= PostfixExpr `:' Annotation {Annotation} ``` An annotated expression `$e$: @$a_1$ $\ldots$ @$a_n$` -attaches [annotations](13-user-defined-annotations.html#user-defined-annotations) $a_1 , \ldots , a_n$ to the +attaches [annotations](11-user-defined-annotations.html#user-defined-annotations) $a_1 , \ldots , a_n$ to the expression $e$. @@ -889,7 +889,7 @@ value of $e_1$. The condition $e_1$ is expected to conform to type `Boolean`. The then-part $e_2$ and the else-part $e_3$ are both expected to conform to the expected type of the conditional expression. The type of the conditional -expression is the [weak least upper bound](05-types.html#weak-conformance) +expression is the [weak least upper bound](03-types.html#weak-conformance) of the types of $e_2$ and $e_3$. A semicolon preceding the `else` symbol of a conditional expression is ignored. @@ -958,7 +958,7 @@ defined by translation to invocations of four methods: `map`, be implemented in different ways for different carrier types. The translation scheme is as follows. In a first step, every -generator `$p$ <- $e$`, where $p$ is not [irrefutable](10-pattern-matching.html#patterns) +generator `$p$ <- $e$`, where $p$ is not [irrefutable](08-pattern-matching.html#patterns) for the type of $e$ is replaced by ```scala @@ -1164,7 +1164,7 @@ Let $\mathit{pt}$ be the expected type of the try expression. The block $b$ is expected to conform to $\mathit{pt}$. The handler $h$ is expected conform to type `scala.PartialFunction[scala.Throwable, $\mathit{pt}\,$]`. The -type of the try expression is the [weak least upper bound](05-types.html#weak-conformance) +type of the try expression is the [weak least upper bound](03-types.html#weak-conformance) of the type of $b$ and the result type of $h$. @@ -1239,9 +1239,9 @@ In that case, a fresh name for the parameter is chosen arbitrarily. A named parameter of an anonymous function may be optionally preceded by an `implicit` modifier. In that case the parameter is -labeled [`implicit`](09-implicit-parameters-and-views.html#implicit-parameters-and-views); however the +labeled [`implicit`](07-implicit-parameters-and-views.html#implicit-parameters-and-views); however the parameter section itself does not count as an implicit parameter -section in the sense defined [here](09-implicit-parameters-and-views.html#implicit-parameters). Hence, arguments to +section in the sense defined [here](07-implicit-parameters-and-views.html#implicit-parameters). Hence, arguments to anonymous functions always have to be given explicitly. ###### Example @@ -1312,13 +1312,13 @@ include at least the expressions of the following forms: - A literal of a value class, such as an integer - A string literal -- A class constructed with [`Predef.classOf`](14-the-scala-standard-library.html#the-predef-object) +- A class constructed with [`Predef.classOf`](12-the-scala-standard-library.html#the-predef-object) - An element of an enumeration from the underlying platform - A literal array, of the form `Array$(c_1 , \ldots , c_n)$`, where all of the $c_i$'s are themselves constant expressions - An identifier defined by a - [constant value definition](06-basic-declarations-and-definitions.html#value-declarations-and-definitions). + [constant value definition](04-basic-declarations-and-definitions.html#value-declarations-and-definitions). ## Statements @@ -1363,7 +1363,7 @@ available implicit conversions are given in the next two sub-sections. We say, a type $T$ is _compatible_ to a type $U$ if $T$ weakly conforms to $U$ after applying [eta-expansion](#eta-expansion) and -[view applications](09-implicit-parameters-and-views.html#views). +[view applications](07-implicit-parameters-and-views.html#views). ### Value Conversions @@ -1393,11 +1393,11 @@ implicitly embedding $e$ in the [type application](#type-applications) `$e$[$T_1 , \ldots , T_n$]`. ###### Numeric Widening -If $e$ has a primitive number type which [weakly conforms](05-types.html#weak-conformance) +If $e$ has a primitive number type which [weakly conforms](03-types.html#weak-conformance) to the expected type, it is widened to the expected type using one of the numeric conversion methods `toShort`, `toChar`, `toInt`, `toLong`, -`toFloat`, `toDouble` defined [here](14-the-scala-standard-library.html#numeric-value-types). +`toFloat`, `toDouble` defined [here](12-the-scala-standard-library.html#numeric-value-types). ###### Numeric Literal Narrowing If the expected type is `Byte`, `Short` or `Char`, and @@ -1412,7 +1412,7 @@ term `{ $e$; () }`. ###### View Application If none of the previous conversions applies, and $e$'s type does not conform to the expected type $\mathit{pt}$, it is attempted to convert -$e$ to the expected type with a [view](09-implicit-parameters-and-views.html#views). +$e$ to the expected type with a [view](07-implicit-parameters-and-views.html#views). ###### Dynamic Member Selection If none of the previous conversions applies, and $e$ is a prefix @@ -1431,7 +1431,7 @@ type $T$ by evaluating the expression to which $m$ is bound. ###### Implicit Application If the method takes only implicit parameters, implicit -arguments are passed following the rules [here](09-implicit-parameters-and-views.html#implicit-parameters). +arguments are passed following the rules [here](07-implicit-parameters-and-views.html#implicit-parameters). ###### Eta Expansion Otherwise, if the method is not a constructor, @@ -1608,7 +1608,7 @@ $T$ is a value type; if it is a method type we apply means finding a substitution $\sigma$ of types $T_i$ for the type parameters $a_i$ such that -- None of inferred types $T_i$ is a [singleton type](05-types.html#singleton-types) +- None of inferred types $T_i$ is a [singleton type](03-types.html#singleton-types) - All type parameter bounds are respected, i.e. $\sigma L_i <: \sigma a_i$ and $\sigma a_i <: \sigma U_i$ for $i = 1 , \ldots , n$. - The expression's type conforms to the expected type, i.e. @@ -1618,7 +1618,7 @@ It is a compile time error if no such substitution exists. If several substitutions exist, local-type inference will choose for each type variable $a_i$ a minimal or maximal type $T_i$ of the solution space. A _maximal_ type $T_i$ will be chosen if the type -parameter $a_i$ appears [contravariantly](06-basic-declarations-and-definitions.html#variance-annotations) in the +parameter $a_i$ appears [contravariantly](04-basic-declarations-and-definitions.html#variance-annotations) in the type $T$ of the expression. A _minimal_ type $T_i$ will be chosen in all other situations, i.e. if the variable appears covariantly, non-variantly or not at all in the type $T$. We call such a substitution @@ -1645,11 +1645,11 @@ constraint system means finding a substitution $\sigma$ of types $T_i$ for the type parameters $a_i$ such that -- None of inferred types $T_i$ is a [singleton type](05-types.html#singleton-types) +- None of inferred types $T_i$ is a [singleton type](03-types.html#singleton-types) - All type parameter bounds are respected, i.e. $\sigma L_i <: \sigma a_i$ and $\sigma a_i <: \sigma U_i$ for $i = 1 , \ldots , n$. - The method's result type $T'$ conforms to the expected type, i.e. $\sigma T' <: \sigma \mathit{pt}$. -- Each argument type [weakly conforms](05-types.html#weak-conformance) +- Each argument type [weakly conforms](03-types.html#weak-conformance) to the corresponding formal parameter type, i.e. $\sigma S_j <:_w \sigma R_j$ for $j = 1 , \ldots , m$. @@ -1657,7 +1657,7 @@ It is a compile time error if no such substitution exists. If several solutions exist, an optimal one for the type $T'$ is chosen. All or parts of an expected type $\mathit{pt}$ may be undefined. The rules for -[conformance](05-types.html#conformance) are extended to this case by adding +[conformance](03-types.html#conformance) are extended to this case by adding the rule that for any type $T$ the following two statements are always true: $\mathit{undefined} <: T$ and $T <: \mathit{undefined}$ diff --git a/spec/09-implicit-parameters-and-views.md b/spec/07-implicit-parameters-and-views.md similarity index 96% rename from spec/09-implicit-parameters-and-views.md rename to spec/07-implicit-parameters-and-views.md index 3e821ec9fa48..1a4d70409c0c 100644 --- a/spec/09-implicit-parameters-and-views.md +++ b/spec/07-implicit-parameters-and-views.md @@ -17,7 +17,7 @@ Template members and parameters labeled with an `implicit` modifier can be passed to [implicit parameters](#implicit-parameters) and can be used as implicit conversions called [views](#views). The `implicit` modifier is illegal for all -type members, as well as for [top-level objects](11-top-level-definitions.html#packagings). +type members, as well as for [top-level objects](09-top-level-definitions.html#packagings). ### Example The following code defines an abstract class of monoids and @@ -62,13 +62,13 @@ call without a prefix and that denote an or an implicit parameter. An eligible identifier may thus be a local name, or a member of an enclosing template, or it may be have been made accessible without a prefix -through an [import clause](06-basic-declarations-and-definitions.html#import-clauses). If there are no eligible +through an [import clause](04-basic-declarations-and-definitions.html#import-clauses). If there are no eligible identifiers under this rule, then, second, eligible are also all `implicit` members of some object that belongs to the implicit scope of the implicit parameter's type, $T$. -The _implicit scope_ of a type $T$ consists of all [companion modules](07-classes-and-objects.html#object-definitions) of classes that are associated with the implicit parameter's type. -Here, we say a class $C$ is _associated_ with a type $T$ if it is a [base class](07-classes-and-objects.html#class-linearization) of some part of $T$. +The _implicit scope_ of a type $T$ consists of all [companion modules](05-classes-and-objects.html#object-definitions) of classes that are associated with the implicit parameter's type. +Here, we say a class $C$ is _associated_ with a type $T$ if it is a [base class](05-classes-and-objects.html#class-linearization) of some part of $T$. The _parts_ of a type $T$ are: @@ -92,7 +92,7 @@ Thus, implicits defined in a package object are part of the implicit scope of a If there are several eligible arguments which match the implicit parameter's type, a most specific one will be chosen using the rules -of static [overloading resolution](08-expressions.html#overloading-resolution). +of static [overloading resolution](06-expressions.html#overloading-resolution). If the parameter has a default argument and no implicit argument can be found the default argument is used. @@ -119,7 +119,7 @@ be passed as implicit parameter. This discussion also shows that implicit parameters are inferred after -any type arguments are [inferred](08-expressions.html#local-type-inference). +any type arguments are [inferred](06-expressions.html#local-type-inference). Implicit methods can themselves have implicit parameters. An example is the following method from module `scala.List`, which injects @@ -182,8 +182,8 @@ To prevent such infinite expansions, the compiler keeps track of a stack of “open implicit types” for which implicit arguments are currently being searched. Whenever an implicit argument for type $T$ is searched, the “core type” of $T$ is added to the stack. Here, the _core type_ -of $T$ is $T$ with aliases expanded, top-level type [annotations](13-user-defined-annotations.html#user-defined-annotations) and -[refinements](05-types.html#compound-types) removed, and occurrences +of $T$ is $T$ with aliases expanded, top-level type [annotations](11-user-defined-annotations.html#user-defined-annotations) and +[refinements](03-types.html#compound-types) removed, and occurrences of top-level existentially bound variables replaced by their upper bounds. The core type is removed from the stack once the search for the implicit argument either definitely fails or succeeds. Everytime a @@ -191,7 +191,7 @@ core type is added to the stack, it is checked that this type does not dominate any of the other types in the set. Here, a core type $T$ _dominates_ a type $U$ if $T$ is -[equivalent](05-types.html#type-equivalence) +[equivalent](03-types.html#type-equivalence) to $U$, or if the top-level type constructors of $T$ and $U$ have a common element and $T$ is more complex than $U$. @@ -433,7 +433,7 @@ Then the following rules apply. `Manifest.intersectionType[T]($ms$)` where $ms$ are the manifests determined for $M[T_1] , \ldots , M[T_n]$. Otherwise, if $M$ is trait `ClassManifest`, - then a manifest is generated for the [intersection dominator](05-types.html#type-erasure) + then a manifest is generated for the [intersection dominator](03-types.html#type-erasure) of the types $T_1 , \ldots , T_n$. 1. If $T$ is some other type, then if $M$ is trait `OptManifest`, a manifest is generated from the designator `scala.reflect.NoManifest`. diff --git a/spec/10-pattern-matching.md b/spec/08-pattern-matching.md similarity index 97% rename from spec/10-pattern-matching.md rename to spec/08-pattern-matching.md index 19182c757b47..7b4d070181e7 100644 --- a/spec/10-pattern-matching.md +++ b/spec/08-pattern-matching.md @@ -109,10 +109,10 @@ expected type of the pattern. SimplePattern ::= StableId ``` -A stable identifier pattern is a [stable identifier](05-types.html#paths) $r$. +A stable identifier pattern is a [stable identifier](03-types.html#paths) $r$. The type of $r$ must conform to the expected type of the pattern. The pattern matches any value $v$ such that -`$r$ == $v$` (see [here](14-the-scala-standard-library.html#root-classes)). +`$r$ == $v$` (see [here](12-the-scala-standard-library.html#root-classes)). To resolve the syntactic overlap with a variable pattern, a stable identifier pattern may not be a simple name starting with a lower-case @@ -151,10 +151,10 @@ SimplePattern ::= StableId `(' [Patterns] `) A constructor pattern is of the form $c(p_1 , \ldots , p_n)$ where $n \geq 0$. It consists of a stable identifier $c$, followed by element patterns $p_1 , \ldots , p_n$. The constructor $c$ is a simple or -qualified name which denotes a [case class](07-classes-and-objects.html#case-classes). +qualified name which denotes a [case class](05-classes-and-objects.html#case-classes). If the case class is monomorphic, then it must conform to the expected type of the pattern, and the formal -parameter types of $x$'s [primary constructor](07-classes-and-objects.html#class-definitions) +parameter types of $x$'s [primary constructor](05-classes-and-objects.html#class-definitions) are taken as the expected types of the element patterns $p_1, \ldots , p_n$. If the case class is polymorphic, then its type parameters are instantiated so that the instantiation of $c$ conforms to the expected @@ -246,7 +246,7 @@ SimplePattern ::= StableId `(' [Patterns `,'] [varid `@'] `_' `*' `)' ``` A pattern sequence $p_1 , \ldots , p_n$ appears in two contexts. -First, in a constructor pattern $c(q_1 , \ldots , q_m, p_1 , \ldots , p_n)$, where $c$ is a case class which has $m+1$ primary constructor parameters, ending in a [repeated parameter](06-basic-declarations-and-definitions.html#repeated-parameters) of type `S*`. +First, in a constructor pattern $c(q_1 , \ldots , q_m, p_1 , \ldots , p_n)$, where $c$ is a case class which has $m+1$ primary constructor parameters, ending in a [repeated parameter](04-basic-declarations-and-definitions.html#repeated-parameters) of type `S*`. Second, in an extractor pattern $x(q_1 , \ldots , q_m, p_1 , \ldots , p_n)$ if the extractor object $x$ does not have an `unapply` method, but it does define an `unapplySeq` method with a result type conforming to `Option[(T_1, ... , T_m, Seq[S])]` (if `m = 0`, the type `Option[Seq[S]]` is also accepted). The expected type for the patterns $p_i$ is $S$. @@ -269,7 +269,7 @@ p_n$. An infix operation pattern $p;\mathit{op};q$ is a shorthand for the constructor or extractor pattern $\mathit{op}(p, q)$. The precedence and associativity of operators in patterns is the same as in -[expressions](08-expressions.html#prefix-infix-and-postfix-operations). +[expressions](06-expressions.html#prefix-infix-and-postfix-operations). An infix operation pattern $p;\mathit{op};(q_1 , \ldots , q_n)$ is a shorthand for the constructor or extractor pattern $\mathit{op}(p, q_1 @@ -289,7 +289,7 @@ matches a value $v$ if at least one its alternatives matches $v$. ### XML Patterns -XML patterns are treated [here](12-xml-expressions-and-patterns.html#xml-patterns). +XML patterns are treated [here](10-xml-expressions-and-patterns.html#xml-patterns). ### Regular Expression Patterns @@ -314,7 +314,7 @@ A pattern $p$ is _irrefutable_ for a type $T$, if one of the following applies: 1. $p$ is a variable pattern, 1. $p$ is a typed pattern $x: T'$, and $T <: T'$, 1. $p$ is a constructor pattern $c(p_1 , \ldots , p_n)$, the type $T$ - is an instance of class $c$, the [primary constructor](07-classes-and-objects.html#class-definitions) + is an instance of class $c$, the [primary constructor](05-classes-and-objects.html#class-definitions) of type $T$ has argument types $T_1 , \ldots , T_n$, and each $p_i$ is irrefutable for $T_i$. @@ -359,7 +359,7 @@ A type pattern $T$ is of one of the following forms: Types which are not of one of the forms described above are also accepted as type patterns. However, such type patterns will be translated to their -[erasure](05-types.html#type-erasure). The Scala +[erasure](03-types.html#type-erasure). The Scala compiler will issue an "unchecked" warning for these patterns to flag the possible loss of type-safety. @@ -486,7 +486,7 @@ x match { Scala does not maintain information about type arguments at run-time, so there is no way to check that `x` is a list of strings. -Instead, the Scala compiler will [erase](05-types.html#type-erasure) the +Instead, the Scala compiler will [erase](03-types.html#type-erasure) the pattern to `List[_]`; that is, it will only test whether the top-level runtime-class of the value `x` conforms to `List`, and the pattern match will succeed if it does. This @@ -565,7 +565,7 @@ instead of $L_i$ and has upper bound $U'_i$ instead of $U_i$. The expected type of every block $b_i$ is the expected type of the whole pattern matching expression. The type of the pattern matching -expression is then the [weak least upper bound](05-types.html#weak-conformance) +expression is then the [weak least upper bound](03-types.html#weak-conformance) of the types of all blocks $b_i$. @@ -592,7 +592,7 @@ side effects in guards. However, it is guaranteed that a guard expression is evaluated only if the pattern it guards matches. If the selector of a pattern match is an instance of a -[`sealed` class](07-classes-and-objects.html#modifiers), +[`sealed` class](05-classes-and-objects.html#modifiers), the compilation of pattern matching can emit warnings which diagnose that a given set of patterns is not exhaustive, i.e. that there is a possibility of a `MatchError` being raised at run-time. @@ -667,7 +667,7 @@ the expression is taken to be equivalent to the anonymous function: ``` Here, each $x_i$ is a fresh name. -As was shown [here](08-expressions.html#anonymous-functions), this anonymous function is in turn +As was shown [here](06-expressions.html#anonymous-functions), this anonymous function is in turn equivalent to the following instance creation expression, where $T$ is the weak least upper bound of the types of all $b_i$. diff --git a/spec/11-top-level-definitions.md b/spec/09-top-level-definitions.md similarity index 98% rename from spec/11-top-level-definitions.md rename to spec/09-top-level-definitions.md index cfb2c98adbfb..b9c78b23a162 100644 --- a/spec/11-top-level-definitions.md +++ b/spec/09-top-level-definitions.md @@ -47,7 +47,7 @@ package $p_1$ { $\ldots$ Every compilation unit implicitly imports the following packages, in the given order: 1. the package `java.lang`, 2. the package `scala`, and - 3. the object [`scala.Predef`](14-the-scala-standard-library.html#the-predef-object), unless there is an explicit top-level import that references `scala.Predef`. + 3. the object [`scala.Predef`](12-the-scala-standard-library.html#the-predef-object), unless there is an explicit top-level import that references `scala.Predef`. Members of a later import in that order hide members of an earlier import. diff --git a/spec/12-xml-expressions-and-patterns.md b/spec/10-xml-expressions-and-patterns.md similarity index 96% rename from spec/12-xml-expressions-and-patterns.md rename to spec/10-xml-expressions-and-patterns.md index 7aab3380d49a..d8c45ecf85d0 100644 --- a/spec/12-xml-expressions-and-patterns.md +++ b/spec/10-xml-expressions-and-patterns.md @@ -16,7 +16,7 @@ changes being mandated by the possibility of embedding Scala code fragments. XML expressions are expressions generated by the following production, where the opening bracket `<` of the first element must be in a position to start the lexical -[XML mode](03-lexical-syntax.html#xml-mode). +[XML mode](01-lexical-syntax.html#xml-mode). ```ebnf XmlExpr ::= XmlContent {Element} @@ -107,7 +107,7 @@ XNameStart ::= ‘_’ | BaseChar | Ideographic XML patterns are patterns generated by the following production, where the opening bracket `<` of the element patterns must be in a position -to start the lexical [XML mode](03-lexical-syntax.html#xml-mode). +to start the lexical [XML mode](01-lexical-syntax.html#xml-mode). ```ebnf XmlPattern ::= ElementPattern @@ -119,7 +119,7 @@ An XML pattern has to be a single element pattern. It matches exactly those runtime representations of an XML tree that have the same structure as described by the pattern. -XML patterns may contain [Scala patterns](10-pattern-matching.html#pattern-matching-expressions). +XML patterns may contain [Scala patterns](08-pattern-matching.html#pattern-matching-expressions). Whitespace is treated the same way as in XML expressions. diff --git a/spec/13-user-defined-annotations.md b/spec/11-user-defined-annotations.md similarity index 99% rename from spec/13-user-defined-annotations.md rename to spec/11-user-defined-annotations.md index 1e631a7ccf3e..fd7a7f9d3fd9 100644 --- a/spec/13-user-defined-annotations.md +++ b/spec/11-user-defined-annotations.md @@ -105,7 +105,7 @@ Java platform, the following annotations have a standard meaning. warning because `Option` is a `sealed` class. * `@uncheckedStable` When applied a value declaration or definition, it allows the defined - value to appear in a path, even if its type is [volatile](05-types.html#volatile-types). + value to appear in a path, even if its type is [volatile](03-types.html#volatile-types). For instance, the following member definitions are legal: ``` diff --git a/spec/14-the-scala-standard-library.md b/spec/12-the-scala-standard-library.md similarity index 98% rename from spec/14-the-scala-standard-library.md rename to spec/12-the-scala-standard-library.md index 4b79fd328514..8868d6e17a2a 100644 --- a/spec/14-the-scala-standard-library.md +++ b/spec/12-the-scala-standard-library.md @@ -140,12 +140,12 @@ Byte - Short `Byte` and `Short` are the lowest-ranked types in this order, whereas `Double` is the highest-ranked. Ranking does _not_ -imply a [conformance relationship](05-types.html#conformance); for +imply a [conformance relationship](03-types.html#conformance); for instance `Int` is not a subtype of `Long`. However, object -[`Predef`](#the-predef-object) defines [views](09-implicit-parameters-and-views.html#views) +[`Predef`](#the-predef-object) defines [views](07-implicit-parameters-and-views.html#views) from every numeric value type to all higher-ranked numeric value types. Therefore, lower-ranked types are implicitly converted to higher-ranked types -when required by the [context](08-expressions.html#implicit-conversions). +when required by the [context](06-expressions.html#implicit-conversions). Given two numeric value types $S$ and $T$, the _operation type_ of $S$ and $T$ is defined as follows: If both $S$ and $T$ are subrange @@ -470,7 +470,7 @@ val ys: Array[Object] = xs.asInstanceOf[Array[Object]] // OK The instantiation of an array with a polymorphic element type $T$ requires information about type $T$ at runtime. -This information is synthesized by adding a [context bound](09-implicit-parameters-and-views.html#context-bounds-and-view-bounds) +This information is synthesized by adding a [context bound](07-implicit-parameters-and-views.html#context-bounds-and-view-bounds) of `scala.reflect.ClassTag` to type $T$. An example is the following implementation of method `mkArray`, which creates @@ -501,7 +501,7 @@ will return a primitive array of `int`s, written as `int[]` in Java. `Array`'s companion object provides various factory methods for the instantiation of single- and multi-dimensional arrays, an extractor method -[`unapplySeq`](10-pattern-matching.html#extractor-patterns) which enables pattern matching +[`unapplySeq`](08-pattern-matching.html#extractor-patterns) which enables pattern matching over arrays and additional utility methods: ```scala @@ -761,7 +761,7 @@ object Predef { The `Predef` object also contains a number of implicit definitions, which are available by default (because `Predef` is implicitly imported). Implicit definitions come in two priorities. High-priority implicits are defined in the `Predef` class itself whereas low priority implicits are defined in a class inherited by `Predef`. The rules of -static [overloading resolution](08-expressions.html#overloading-resolution) +static [overloading resolution](06-expressions.html#overloading-resolution) stipulate that, all other things being equal, implicit resolution prefers high-priority implicits over low-priority ones. diff --git a/spec/15-syntax-summary.md b/spec/13-syntax-summary.md similarity index 100% rename from spec/15-syntax-summary.md rename to spec/13-syntax-summary.md diff --git a/spec/16-references.md b/spec/14-references.md similarity index 100% rename from spec/16-references.md rename to spec/14-references.md diff --git a/spec/_layouts/toc.yml b/spec/_layouts/toc.yml new file mode 100644 index 000000000000..d77ea62a19f1 --- /dev/null +++ b/spec/_layouts/toc.yml @@ -0,0 +1,15 @@ + + + + + + + + + + + +{{ content }} + + + \ No newline at end of file diff --git a/spec/index.md b/spec/index.md index 3cadbdee8338..22209b42629c 100644 --- a/spec/index.md +++ b/spec/index.md @@ -1,14 +1,71 @@ --- title: Scala Language Reference -layout: default +layout: toc --- - + + + +## Preface + +Scala is a Java-like programming language which unifies +object-oriented and functional programming. It is a pure +object-oriented language in the sense that every value is an +object. Types and behavior of objects are described by +classes. Classes can be composed using mixin composition. Scala is +designed to work seamlessly with two less pure but mainstream +object-oriented languages – Java and C#. + +Scala is a functional language in the sense that every function is a +value. Nesting of function definitions and higher-order functions are +naturally supported. Scala also supports a general notion of pattern +matching which can model the algebraic types used in many functional +languages. + +Scala has been designed to interoperate seamlessly with Java. +Scala classes can call Java methods, create Java objects, inherit from Java +classes and implement Java interfaces. None of this requires interface +definitions or glue code. + +Scala has been developed from 2001 in the programming methods +laboratory at EPFL. Version 1.0 was released in November 2003. This +document describes the second version of the language, which was +released in March 2006. It acts a reference for the language +definition and some core library modules. It is not intended to teach +Scala or its concepts; for this there are [other documents](14-references.html). + +Scala has been a collective effort of many people. The design and the +implementation of version 1.0 was completed by Philippe Altherr, +Vincent Cremet, Gilles Dubochet, Burak Emir, Stéphane Micheloud, +Nikolay Mihaylov, Michel Schinz, Erik Stenman, Matthias Zenger, and +the author. Iulian Dragos, Gilles Dubochet, Philipp Haller, Sean +McDirmid, Lex Spoon, and Geoffrey Washburn joined in the effort to +develop the second version of the language and tools. Gilad Bracha, +Craig Chambers, Erik Ernst, Matthias Felleisen, Shriram Krishnamurti, +Gary Leavens, Sebastian Maneth, Erik Meijer, Klaus Ostermann, Didier +Rémy, Mads Torgersen, and Philip Wadler have shaped the design of +the language through lively and inspiring discussions and comments on +previous versions of this document. The contributors to the Scala +mailing list have also given very useful feedback that helped us +improve the language and its tools.