diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 474b6c4a6030..210bc5815f69 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -3541,12 +3541,8 @@ object Parsers { /** Def ::= val PatDef * | var VarDef * | def DefDef - * | type {nl} TypeDcl + * | type {nl} TypeDef * | TmplDef - * Dcl ::= val ValDcl - * | var ValDcl - * | def DefDcl - * | type {nl} TypeDcl * EnumCase ::= `case' (id ClassConstr [`extends' ConstrApps]] | ids) */ def defOrDcl(start: Int, mods: Modifiers): Tree = in.token match { @@ -3567,12 +3563,10 @@ object Parsers { tmplDef(start, mods) } - /** PatDef ::= ids [‘:’ Type] ‘=’ Expr - * | Pattern2 [‘:’ Type] ‘=’ Expr + /** PatDef ::= ids [‘:’ Type] [‘=’ Expr] + * | Pattern2 [‘:’ Type] [‘=’ Expr] * VarDef ::= PatDef - * | id {`,' id} `:' Type `=' `_' (deprecated in 3.x) - * ValDcl ::= id {`,' id} `:' Type - * VarDcl ::= id {`,' id} `:' Type + * | id {`,' id} `:' Type `=' `_' (deprecated in 3.x) */ def patDefOrDcl(start: Offset, mods: Modifiers): Tree = atSpan(start, nameStart) { val first = pattern2() @@ -3621,9 +3615,8 @@ object Parsers { } } - /** DefDef ::= DefSig [‘:’ Type] ‘=’ Expr + /** DefDef ::= DefSig [‘:’ Type] [‘=’ Expr] * | this TypelessClauses [DefImplicitClause] `=' ConstrExpr - * DefDcl ::= DefSig `:' Type * DefSig ::= id [DefTypeParamClause] DefTermParamClauses * * if clauseInterleaving is enabled: @@ -3721,7 +3714,7 @@ object Parsers { argumentExprss(mkApply(Ident(nme.CONSTRUCTOR), argumentExprs())) } - /** TypeDcl ::= id [TypeParamClause] {FunParamClause} TypeBounds [‘=’ Type] + /** TypeDef ::= id [TypeParamClause] {FunParamClause} TypeBounds [‘=’ Type] */ def typeDefOrDcl(start: Offset, mods: Modifiers): Tree = { newLinesOpt() @@ -4208,7 +4201,6 @@ object Parsers { * TemplateStat ::= Import * | Export * | Annotations Modifiers Def - * | Annotations Modifiers Dcl * | Extension * | Expr1 * | @@ -4238,10 +4230,10 @@ object Parsers { } /** RefineStatSeq ::= RefineStat {semi RefineStat} - * RefineStat ::= ‘val’ VarDcl - * | ‘def’ DefDcl - * | ‘type’ {nl} TypeDcl - * (in reality we admit Defs and vars and filter them out afterwards in `checkLegal`) + * RefineStat ::= ‘val’ VarDef + * | ‘def’ DefDef + * | ‘type’ {nl} TypeDef + * (in reality we admit class defs and vars and filter them out afterwards in `checkLegal`) */ def refineStatSeq(): List[Tree] = { val stats = new ListBuffer[Tree] diff --git a/docs/_docs/internals/syntax.md b/docs/_docs/internals/syntax.md index ace038033f7e..85621a66a366 100644 --- a/docs/_docs/internals/syntax.md +++ b/docs/_docs/internals/syntax.md @@ -208,7 +208,7 @@ ParamType ::= [‘=>’] ParamValueType ParamValueType ::= [‘into’] ExactParamType Into(t) ExactParamType ::= ParamValueType [‘*’] PostfixOp(t, "*") TypeArgs ::= ‘[’ Types ‘]’ ts -Refinement ::= :<<< [RefineDcl] {semi [RefineDcl]} >>> ds +Refinement ::= :<<< [RefineDef] {semi [RefineDef]} >>> ds TypeBounds ::= [‘>:’ Type] [‘<:’ Type] TypeBoundsTree(lo, hi) TypeParamBounds ::= TypeBounds {‘:’ Type} ContextBounds(typeBounds, tps) Types ::= Type {‘,’ Type} @@ -412,29 +412,24 @@ EndMarkerTag ::= id | ‘if’ | ‘while’ | ‘for’ | ‘match’ | | ‘new’ | ‘this’ | ‘given’ | ‘extension’ | ‘val’ ``` -### Declarations and Definitions +### Definitions ```ebnf -RefineDcl ::= ‘val’ ValDcl - | ‘def’ DefDcl - | ‘type’ {nl} TypeDcl -Dcl ::= RefineDcl - | ‘var’ VarDcl -ValDcl ::= ids ‘:’ Type PatDef(_, ids, tpe, EmptyTree) -VarDcl ::= ids ‘:’ Type PatDef(_, ids, tpe, EmptyTree) -DefDcl ::= DefSig ‘:’ Type DefDef(_, name, paramss, tpe, EmptyTree) -DefSig ::= id [DefParamClauses] [DefImplicitClause] -TypeDcl ::= id [TypeParamClause] {FunParamClause} TypeBounds TypeDefTree(_, name, tparams, bound - [‘=’ Type] +RefineDef ::= ‘val’ ValDef + | ‘def’ DefDef + | ‘type’ {nl} TypeDef Def ::= ‘val’ PatDef | ‘var’ PatDef | ‘def’ DefDef - | ‘type’ {nl} TypeDcl + | ‘type’ {nl} TypeDef | TmplDef -PatDef ::= ids [‘:’ Type] ‘=’ Expr - | Pattern2 [‘:’ Type] ‘=’ Expr PatDef(_, pats, tpe?, expr) -DefDef ::= DefSig [‘:’ Type] ‘=’ Expr DefDef(_, name, paramss, tpe, expr) +PatDef ::= ids [‘:’ Type] [‘=’ Expr] + | Pattern2 [‘:’ Type] [‘=’ Expr] PatDef(_, pats, tpe?, expr) +DefDef ::= DefSig [‘:’ Type] [‘=’ Expr] DefDef(_, name, paramss, tpe, expr) | ‘this’ TypelessClauses [DefImplicitClause] ‘=’ ConstrExpr DefDef(_, , vparamss, EmptyTree, expr | Block) +DefSig ::= id [DefParamClauses] [DefImplicitClause] +TypeDef ::= id [TypeParamClause] {FunParamClause} TypeBounds TypeDefTree(_, name, tparams, bound + [‘=’ Type] TmplDef ::= ([‘case’] ‘class’ | ‘trait’) ClassDef | [‘case’] ‘object’ ObjectDef diff --git a/docs/_spec/01-lexical-syntax.md b/docs/_spec/01-lexical-syntax.md index 370d80955545..a2b5c4c423a9 100644 --- a/docs/_spec/01-lexical-syntax.md +++ b/docs/_spec/01-lexical-syntax.md @@ -218,13 +218,13 @@ Multiple newline tokens are accepted in the following places (note that a semico - between the condition of a [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](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](04-basic-declarations-and-definitions.html#type-declarations-and-type-aliases). +- after the initial `type` keyword in a [type definition](04-basic-definitions.html#type-member-definitions). 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](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](04-basic-declarations-and-definitions.html#function-declarations-and-definitions), and +- in front of a [parameter clause](04-basic-definitions.html#method-definitions), and - after an [annotation](11-annotations.html#user-defined-annotations). > The newline tokens between the two lines are not treated as statement separators. diff --git a/docs/_spec/02-identifiers-names-and-scopes.md b/docs/_spec/02-identifiers-names-and-scopes.md index 9d52cb1654b5..551781e911d0 100644 --- a/docs/_spec/02-identifiers-names-and-scopes.md +++ b/docs/_spec/02-identifiers-names-and-scopes.md @@ -8,15 +8,15 @@ chapter: 2 Names in Scala identify types, values, methods, and classes which are collectively called _entities_. Names are introduced by local -[definitions and declarations](04-basic-declarations-and-definitions.html#basic-declarations-and-definitions), +[definitions](04-basic-definitions.html#basic-definitions), [inheritance](05-classes-and-objects.html#class-members), -[import clauses](04-basic-declarations-and-definitions.html#import-clauses), or +[import clauses](04-basic-definitions.html#import-clauses), or [package clauses](09-top-level-definitions.html#packagings) which are collectively called _bindings_. Bindings of different kinds have precedence defined on them: -1. Definitions and declarations that are local, inherited, or made available by a package clause and also defined in the same compilation unit as the reference to them, have the highest precedence. +1. Definitions that are local, inherited, or made available by a package clause and also defined in the same compilation unit as the reference to them, have the highest precedence. 1. Explicit imports have the next highest precedence. 1. Wildcard imports have the next highest precedence. 1. Definitions made available by a package clause, but not also defined in the same compilation unit as the reference to them, as well as imports which are supplied by the compiler but not explicitly written in source code, have the lowest precedence. @@ -48,7 +48,7 @@ A reference to an unqualified (type- or term-) identifier ´x´ is bound by the 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. +If ´x´ is bound by a definition, 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. diff --git a/docs/_spec/03-types.md b/docs/_spec/03-types.md index 809999bbcdaf..1cf58b789182 100644 --- a/docs/_spec/03-types.md +++ b/docs/_spec/03-types.md @@ -35,7 +35,7 @@ SimpleRef ::= id ParamType ::= [‘=>’] ParamValueType ParamValueType ::= ParamValueType [‘*’] TypeArgs ::= ‘[’ TypesOrWildcards ‘]’ -Refinement ::= :<<< [RefineDcl] {semi [RefineDcl]} >>> +Refinement ::= :<<< [RefineDef] {semi [RefineDef]} >>> FunTypeArgs ::= InfixType | ‘(’ [ FunArgTypes ] ‘)’ @@ -51,9 +51,9 @@ TypeLambdaParam ::= {Annotation} (id | ‘_’) [TypeParamClause] TypeBou TypeParamClause ::= ‘[’ VariantTypeParam {‘,’ VariantTypeParam} ‘]’ VariantTypeParam ::= {Annotation} [‘+’ | ‘-’] (id | ‘_’) [TypeParamClause] TypeBounds -RefineDcl ::= ‘val’ ValDcl - | ‘def’ DefDcl - | ‘type’ {nl} TypeDcl +RefineDef ::= ‘val’ ValDef + | ‘def’ DefDef + | ‘type’ {nl} TypeDef TypeBounds ::= [‘>:’ Type] [‘<:’ Type] @@ -193,11 +193,11 @@ TypedFunParam ::= id ‘:’ Type The concrete function type ´(T_1, ..., T_n) \Rightarrow R´ represents the set of function values that take arguments of types ´T_1, ..., Tn´ and yield results of type ´R´. The case of exactly one argument type ´T \Rightarrow R´ is a shorthand for ´(T) \Rightarrow R´. -An argument type of the form ´\Rightarrow T´ represents a [call-by-name parameter](04-basic-declarations-and-definitions.html#by-name-parameters) of type ´T´. +An argument type of the form ´\Rightarrow T´ represents a [call-by-name parameter](04-basic-definitions.html#by-name-parameters) of type ´T´. Function types associate to the right, e.g. ´S \Rightarrow T \Rightarrow R´ is the same as ´S \Rightarrow (T \Rightarrow R)´. -Function types are [covariant](04-basic-declarations-and-definitions.md#variance-annotations) in their result type and [contravariant](04-basic-declarations-and-definitions.md#variance-annotations) in their argument types. +Function types are [covariant](04-basic-definitions.md#variance-annotations) in their result type and [contravariant](04-basic-definitions.md#variance-annotations) in their argument types. Function types translate into internal class types that define an `apply` method. Specifically, the ´n´-ary function type ´(T_1, ..., T_n) \Rightarrow R´ translates to the internal class type `scala.Function´_n´[´T_1´, ..., ´T_n´, ´R´]`. @@ -253,23 +253,24 @@ Notes: RefinedType ::= AnnotType {[nl] Refinement} SimpleType1 ::= ... | Refinement -Refinement ::= :<<< [RefineDcl] {semi [RefineDcl]} >>> +Refinement ::= :<<< [RefineDef] {semi [RefineDef]} >>> -RefineDcl ::= ‘val’ ValDcl - | ‘def’ DefDcl - | ‘type’ {nl} TypeDcl +RefineDef ::= ‘val’ ValDef + | ‘def’ DefDef + | ‘type’ {nl} TypeDef ``` -In the concrete syntax of types, refinements can contain several refined declarations. -Moreover, the refined declarations can refer to each other as well as to members of the parent type, i.e., they have access to `this`. +In the concrete syntax of types, refinements can contain several refined definitions. +They must all be abstract. +Moreover, the refined definitions can refer to each other as well as to members of the parent type, i.e., they have access to `this`. -In the internal types, each refinement defines exactly one refined declaration, and references to `this` must be made explicit in a recursive type. +In the internal types, each refinement defines exactly one refined definition, and references to `this` must be made explicit in a recursive type. The conversion from the concrete syntax to the abstract syntax works as follows: 1. Create a fresh recursive this name ´\alpha´. -2. Replace every implicit or explicit reference to `this` in the refinement declarations by ´\alpha´. -3. Create nested [refined types](#refined-types), one for every refined declaration. +2. Replace every implicit or explicit reference to `this` in the refinement definitions by ´\alpha´. +3. Create nested [refined types](#refined-types), one for every refined definition. 4. Unless ´\alpha´ was never actually used, wrap the result in a [recursive type](#recursive-types) `{ ´\alpha´ => ´...´ }`. ### Concrete Type Lambdas @@ -361,7 +362,7 @@ To each type constructor corresponds an _inferred type parameter clause_ which i ### Type Definitions -A _type definition_ ´D´ represents the right-hand-side of a `type` declaration or the bounds of a type parameter. +A _type definition_ ´D´ represents the right-hand-side of a `type` member definition or the bounds of a type parameter. It is either: - a type alias of the form ´= U´, or @@ -465,7 +466,7 @@ If the class is monomorphic, the type designator is a value type denoting the se Otherwise it is a type constructor with the same type parameters as the class definition. All class types are concrete, non-stable types. -If a type designator ´p.T´ is not a class type, it refers to a type definition `T` (a type parameter or a `type` declaration) and has an _underlying [type definition](#type-definitions)_. +If a type designator ´p.T´ is not a class type, it refers to a type definition `T` (a type parameter or a `type` member definition) and has an _underlying [type definition](#type-definitions)_. If ´p = \epsilon´ or ´p´ is a package ref, the underlying type definition is the _declared type definition_ of `T`. Otherwise, it is determined by [`memberType`](#member-type)`(´p´, ´T´)`. A non-class type designator is concrete (resp. stable) if and only if its underlying type definition is an alias ´U´ and ´U´ is itself concrete (resp. stable). @@ -860,7 +861,7 @@ If a method name is used as a value, its type is [implicitly converted](06-expre ###### Example -The declarations +The definitions ```scala def a: Int @@ -889,7 +890,7 @@ This type represents named methods that take type arguments `´S_1, ..., S_n´` ###### Example -The declarations +The definitions ```scala def empty[A]: List[A] @@ -1030,7 +1031,7 @@ We define `memberType(´T´, ´id´, ´p´)` as follows: - If ´T´ is a possibly parameterized class type of the form ´q.C[T_1, ..., T_n]´ (with ´n \geq 0´): - Let ´m´ be the [class member](05-classes-and-objects.html#class-members) of ´C´ with name ´id´. - If ´m´ is not defined, the result is undefined. - - If ´m´ is a class declaration, the result is a class result with class ´m´. + - If ´m´ is a class definition, the result is a class result with class ´m´. - If ´m´ is a term definition in class ´D´ with declared type ´U´, the result is a term result with underlying type [`asSeenFrom`](#as-seen-from)`(´U´, ´D´, ´p´)` and stable flag true if and only if ´m´ is stable. - If ´m´ is a type member definition in class ´D´, the result is a type result with underlying type definition [`asSeenFrom`](#as-seen-from)`(´U´, ´D´, ´p´)` where ´U´ is defined as follows: - If ´m´ is an opaque type alias member definition with declared definition ´>: L <: H = V´, then diff --git a/docs/_spec/04-basic-declarations-and-definitions.md b/docs/_spec/04-basic-definitions.md similarity index 81% rename from docs/_spec/04-basic-declarations-and-definitions.md rename to docs/_spec/04-basic-definitions.md index 4feeb34eebe5..369709b52bff 100644 --- a/docs/_spec/04-basic-declarations-and-definitions.md +++ b/docs/_spec/04-basic-definitions.md @@ -1,16 +1,12 @@ --- -title: Basic Declarations & Definitions +title: Basic Definitions layout: default chapter: 4 --- -# Basic Declarations and Definitions +# Basic Definitions ```ebnf -Dcl ::= ‘val’ ValDcl - | ‘var’ VarDcl - | ‘def’ FunDcl - | ‘type’ {nl} TypeDcl PatVarDef ::= ‘val’ PatDef | ‘var’ VarDef Def ::= PatVarDef @@ -20,20 +16,18 @@ Def ::= PatVarDef | TmplDef ``` -A _declaration_ introduces names and assigns them types or type definitions. -It can form part of a [class definition](05-classes-and-objects.html#templates) or of a refinement in a [refined type](03-types.html#concrete-refined-types). +A _definition_ introduces names that denote terms and assigns them types, or that denote types and assigns them [type definitions](./03-types.html#type-definitions). +It can form part of an object or [class definition](05-classes-and-objects.html#templates) or it can be local to a block. -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 block. -Both declarations and definitions produce _bindings_ that associate type names with type definitions or bounds, and that associate term names with types. - -The scope of a name introduced by a declaration or definition is the whole statement sequence containing the binding. +The scope of a name introduced by a definition is the whole statement sequence containing the definition. However, there is a restriction on forward references in blocks: In a statement sequence ´s_1 ... s_n´ making up a block, if a simple name in ´s_i´ refers to an entity defined by ´s_j´ where ´j \geq i´, then for all ´s_k´ between and including ´s_i´ and ´s_j´, - ´s_k´ cannot be a variable definition. - If ´s_k´ is a value definition, it must be lazy. +Moreover, in a block, all term definitions must be concrete, and opaque type alias definitions are not allowed. + -## Value Declarations and Definitions +## Value Definitions ```ebnf -Dcl ::= ‘val’ ValDcl -ValDcl ::= ids ‘:’ Type PatVarDef ::= ‘val’ PatDef -PatDef ::= Pattern2 {‘,’ Pattern2} [‘:’ Type] ‘=’ Expr +PatDef ::= Pattern2 {‘,’ Pattern2} [‘:’ Type] [‘=’ Expr] ids ::= id {‘,’ id} ``` -A value declaration `val ´x´: ´T´` introduces ´x´ as a name of a value of _declared type_ ´T´. +An abstract value definition `val ´x´: ´T´` introduces ´x´ as a name of a value of _declared type_ ´T´. +´T´ must be explicitly specified and must be a [proper type](03-types.html#proper-types). -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 declared type ´T´ may be omitted, in which case the [packed type](06-expressions.html#expression-typing) of expression ´e´ is assumed. +A concrete 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 declared type ´T´ may be omitted, in which case the [packed type](06-expressions.html#expression-typing) of the expression ´e´ is assumed. If a type ´T´ is given, then it must be a [proper type](03-types.html#proper-types) and ´e´ is expected to [conform to it](06-expressions.html#expression-typing). Evaluation of the value definition implies evaluation of its right-hand side ´e´, unless it has the modifier `lazy`. -The effect of the value definition is to bind ´x´ to the value of ´e´ -converted to type ´T´. +The effect of the value definition is to bind ´x´ to the value of ´e´ converted to type ´T´. A `lazy` value definition evaluates its right hand side ´e´ the first time the value is accessed. A _constant value definition_ is of the form @@ -83,7 +75,7 @@ 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](08-pattern-matching.html#patterns) as left-hand side. +Concrete 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: 1. If the pattern ´p´ has bound variables ´x_1, ..., x_n´, where ´n > 1´: @@ -106,7 +98,7 @@ val ´x´ = ´e´ match { case ´p´ => ´x´ } 3. If ´p´ has no bound variables: ```scala -´e´ match { case ´p´ => ()} +´e´ match { case ´p´ => () } ``` ###### Example @@ -114,6 +106,7 @@ val ´x´ = ´e´ match { case ´p´ => ´x´ } The following are examples of value definitions ```scala +val foo: Int // abstract value definition val pi = 3.1415 val pi: Double = 3.1415 // equivalent to first definition val Some(x) = f() // a pattern definition @@ -130,13 +123,13 @@ val x = x´\$´._1 val xs = x´\$´._2 ``` -The name of any declared or defined value may not end in `_=`. +The name of any defined value may not end in `_=`. -A value declaration `val ´x_1, ..., x_n´: ´T´` is a shorthand for the sequence of value declarations `val ´x_1´: ´T´; ...; val ´x_n´: ´T´`. +A value definition `val ´x_1, ..., x_n´: ´T´` is a shorthand for the sequence of value definitions `val ´x_1´: ´T´; ...; val ´x_n´: ´T´`. A value definition `val ´p_1, ..., p_n´ = ´e´` is a shorthand for the sequence of value definitions `val ´p_1´ = ´e´; ...; val ´p_n´ = ´e´`. A value definition `val ´p_1, ..., p_n: T´ = ´e´` is a shorthand for the sequence of value definitions `val ´p_1: T´ = ´e´; ...; val ´p_n: T´ = ´e´`. -## Variable Declarations and Definitions +## Variable Definitions ```ebnf Dcl ::= ‘var’ VarDcl @@ -146,23 +139,23 @@ VarDef ::= PatDef | ids ‘:’ Type ‘=’ ‘_’ ``` -A variable declaration `var ´x´: ´T´` is equivalent to the declarations of both a _getter method_ ´x´ *and* a _setter method_ `´x´_=`: +An abstract variable definition `var ´x´: ´T´` is equivalent to the definition of both a _getter method_ ´x´ *and* a _setter method_ `´x´_=`: ```scala def ´x´: ´T´ def ´x´_= (´y´: ´T´): Unit ``` -An implementation of a class may _define_ a declared variable using a variable definition, or by defining the corresponding setter and getter methods. +An implementation of a class may implement a defined abstract variable using a concrete variable definition, or by defining the corresponding setter and getter methods. -A variable definition `var ´x´: ´T´ = ´e´` introduces a mutable variable with type ´T´ and initial value as given by the expression ´e´. +A concrete 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 it must be a [proper type](03-types.html#proper-types) and ´e´ is expected to [conform to it](06-expressions.html#expression-typing). 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 as a [value definition](#value-declarations-and-definitions) `val ´p´ = ´e´`, except that the free names in ´p´ are introduced as mutable variables, not values. +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 as a [value definition](#value-definitions) `val ´p´ = ´e´`, except that the free names in ´p´ are introduced as mutable variables, not values. -The name of any declared or defined variable may not end in `_=`. +The name of any defined variable may not end in `_=`. The right-hand-side of a mutable variable definition that is a member of a template can be the special reference `scala.compiletime.uninitialized`: `var ´x´: ´T´ = scala.compiletime.uninitialized`. It introduces a mutable field with type ´T´ and a default initial value. @@ -181,9 +174,9 @@ The default value depends on the type ´T´ as follows: `scala.compiletime.uninitialized` can never appear anywhere else. For compatibility with Scala 2, the syntax `var ´x´: ´T´ = _` is accepted as equivalent to using `uninitialized`. -When they occur as members of a template, both forms of variable definition also introduce a getter method ´x´ which returns the value currently assigned to the variable, as well as a setter method `´x´_=` which changes the value currently assigned to the variable. -The methods have the same signatures as for a variable declaration. -The template then has these getter and setter methods as members, whereas the original variable cannot be accessed directly as a template member. +When they occur as members of a template, both forms of concrete variable definition also introduce a setter method `´x´_=` which changes the value currently assigned to the variable. +The setter has the same signatures as for an abstract variable definition. +It is then not possible to directly modify the value assigned to the variable; mutations always go through the corresponding setter. ###### Example @@ -215,11 +208,11 @@ d.hours = 8; d.minutes = 30; d.seconds = 0 d.hours = 25 // throws a DateError exception ``` -A variable declaration `var ´x_1, ..., x_n´: ´T´` is a shorthand for the sequence of variable declarations `var ´x_1´: ´T´; ...; var ´x_n´: ´T´`. +A variable definition `var ´x_1, ..., x_n´: ´T´` is a shorthand for the sequence of variable definitions `var ´x_1´: ´T´; ...; var ´x_n´: ´T´`. A variable definition `var ´x_1, ..., x_n´ = ´e´` is a shorthand for the sequence of variable definitions `var ´x_1´ = ´e´; ...; var ´x_n´ = ´e´`. A variable definition `var ´x_1, ..., x_n: T´ = ´e´` is a shorthand for the sequence of variable definitions `var ´x_1: T´ = ´e´; ...; var ´x_n: T´ = ´e´`. -## Type Declarations and Type Aliases +## Type Member Definitions ```ebnf Dcl ::= ‘type’ {nl} TypeDcl @@ -230,23 +223,25 @@ TypeDef ::= id [TypeParamClause] ‘=’ Type OpaqueTypeDef ::= id [TypeParamClause] [‘>:’ Type] [‘<:’ Type] ‘=’ Type ``` -A possibly parameterized _type declaration_ `type ´t´[´\mathit{tps}\,´] >: ´L´ <: ´H´` declares ´t´ to be an abstract type. +_Type members_ can be abstract type members, type aliases, or opaque type aliases. + +A possibly parameterized _abstract type member_ definition `type ´t´[´\mathit{tps}\,´] >: ´L´ <: ´H´` declares ´t´ to be an abstract type. If omitted, ´L´ and ´H´ are implied to be `Nothing` and `scala.Any`, respectively. -A possibly parameterized _type alias_ `type ´t´[´\mathit{tps}\,´] = ´T´` defines ´t´ to be a concrete type member. +A possibly parameterized _type alias_ definition `type ´t´[´\mathit{tps}\,´] = ´T´` defines ´t´ to be a concrete type member. -A possibly parameterized _opaque type alias_ `opaque type ´t´[´\mathit{tps}\,´] >: ´L´ <: ´H´ = ´T´` defines ´t´ to be an opaque type alias with public bounds `>: ´L´ <: ´H´` and a private alias `= ´T´`. +A possibly parameterized _opaque type alias_ definition `opaque type ´t´[´\mathit{tps}\,´] >: ´L´ <: ´H´ = ´T´` defines ´t´ to be an opaque type alias with public bounds `>: ´L´ <: ´H´` and a private alias `= ´T´`. If a type parameter clause `[´\mathit{tps}\,´]` is present, it is desugared away according to the rules in the following section. -### Desugaring of parameterized type declarations +### Desugaring of parameterized type definitions -A parameterized type declaration is desugared into an unparameterized type declaration whose bounds are [type lambdas](03-types.html#type-lambdas) with explicit variance annotations. +A parameterized type definition is desugared into an unparameterized type definition whose bounds are [type lambdas](03-types.html#type-lambdas) with explicit variance annotations. The scope of a type parameter extends over the bounds `>: ´L´ <: ´U´` or the alias `= ´T´` and the type parameter clause ´\mathit{tps}´ itself. -A higher-order type parameter clause (of an abstract type constructor ´tc´) has the same kind of scope, restricted to the declaration of the type parameter ´tc´. +A higher-order type parameter clause (of an abstract type constructor ´tc´) has the same kind of scope, restricted to the definition of the type parameter ´tc´. -To illustrate nested scoping, these declarations are all equivalent: `type t[m[x] <: Bound[x], Bound[x]]`, `type t[m[x] <: Bound[x], Bound[y]]` and `type t[m[x] <: Bound[x], Bound[_]]`, as the scope of, e.g., the type parameter of ´m´ is limited to the declaration of ´m´. +To illustrate nested scoping, these definitions are all equivalent: `type t[m[x] <: Bound[x], Bound[x]]`, `type t[m[x] <: Bound[x], Bound[y]]` and `type t[m[x] <: Bound[x], Bound[_]]`, as the scope of, e.g., the type parameter of ´m´ is limited to the definition of ´m´. In all of them, ´t´ is an abstract type member that abstracts over two type constructors: ´m´ stands for a type constructor that takes one type parameter and that must be a subtype of `Bound`, ´t´'s second type constructor parameter. `t[MutableList, Iterable]` is a valid use of ´t´. @@ -313,16 +308,16 @@ type ´t´ >: [´\mathit{tps'}\,´] =>> ´L´ <: [´\mathit{tps'}\,´] =>> ´H´ ``` where ´\mathit{tps'}´ is computed as in the previous cases. -### Non-Parameterized Type Declarations and Type Aliases +### Non-Parameterized Type Member Definitions -A _type declaration_ `type ´t´ >: ´L´ <: ´H´` declares ´t´ to be an abstract type whose [type definition](03-types.html#type-definitions) has the lower bound type ´L´ and upper bound type ´H´. +An _abstract type member_ definition `type ´t´ >: ´L´ <: ´H´` declares ´t´ to be an abstract type whose [type definition](03-types.html#type-definitions) has the lower bound type ´L´ and upper bound type ´H´. -If a type declaration appears as a member declaration of a type, implementations of the type may implement ´t´ with any type ´T´ for which ´L <: T <: H´. +If a type definition appears as a member definition of a type, implementations of the type may implement ´t´ with any type ´T´ for which ´L <: T <: H´. It is a compile-time error if ´L´ does not conform to ´H´. -A _type alias_ `type ´t´ = ´T´` defines ´t´ to be an alias name for the type ´T´. +A _type alias_ definition `type ´t´ = ´T´` defines ´t´ to be an alias name for the type ´T´. -An _opaque type alias_ `opaque type ´t´ >: ´L´ <: ´H´ = ´T´` defines ´t´ to be an opaque type alias with public bounds `>: ´L´ <: ´H´` and a private alias `= ´T´`. +An _opaque type alias_ definition `opaque type ´t´ >: ´L´ <: ´H´ = ´T´` defines ´t´ to be an opaque type alias with public bounds `>: ´L´ <: ´H´` and a private alias `= ´T´`. An opaque type alias can only be declared within a [template](./05-classes-and-objects.html#templates). It cannot be `private` and cannot be overridden in subclasses. In order for the definition to be valid, ´T´ must satisfy some constraints: @@ -332,17 +327,17 @@ In order for the definition to be valid, ´T´ must satisfy some constraints: - If ´T´ is a type lambda, its result must be a proper type (i.e., it cannot be a curried type lambda). When viewed from within its enclosing template, an opaque type alias behaves as a type alias with type definition `= ´T´`. -When viewed from anywhere else, it behaves as a type declaration with type definition `>: ´L´ <: ´H´`. +When viewed from anywhere else, it behaves as an abstract type member with type definition `>: ´L´ <: ´H´`. See [`memberType`](./03-types.html#member-type) for the precise mechanism that governs this dual view. -The scope rules for [definitions](#basic-declarations-and-definitions) and [type parameters](#method-declarations-and-definitions) make it possible that a type name appears in its own bounds or in its right-hand side. +The scope rules for [definitions](#basic-definitions) and [type parameters](#method-definitions) make it possible that a type name appears in its own bounds or in its right-hand side. However, it is a static error if a type alias refers recursively to the defined type itself. That is, the type ´T´ in a type alias `type ´t´[´\mathit{tps}\,´] = ´T´` may not refer directly or indirectly to the name ´t´. It is also an error if an abstract type is directly or indirectly its own upper or lower bound. ###### Example -The following are legal type declarations and definitions: +The following are legal type definitions: ```scala type IntList = List[Integer] @@ -457,7 +452,7 @@ Variance annotations indicate how instances of parameterized types vary with res A ‘+’ variance indicates a covariant dependency, a ‘-’ variance indicates a contravariant dependency, and a missing variance indication indicates an invariant dependency. A variance annotation constrains the way the annotated type variable may appear in the type or class which binds the type parameter. -In a type definition `type ´T´[´\mathit{tps}\,´] = ´S´`, or a type declaration `type ´T´[´\mathit{tps}\,´] >: ´L´ <: ´U´` type parameters labeled ‘+’ must only appear in covariant position whereas type parameters labeled ‘-’ must only appear in contravariant position. +In a type definition `type ´T´[´\mathit{tps}\,´] = ´S´`, `type ´T´[´\mathit{tps}\,´] >: ´L´ <: ´U´` or `opaque type ´T´[´\mathit{tps}\,´] >: ´L´ <: ´U´ = ´S´`, type parameters labeled ‘+’ must only appear in covariant position whereas type parameters labeled ‘-’ must only appear in contravariant position. Analogously, for a class definition `class ´C´[´\mathit{tps}\,´](´\mathit{ps}\,´) extends ´T´ { ´x´: ´S´ => ...}`, type parameters labeled ‘+’ must only appear in covariant position in the self type ´S´ and the template ´T´, whereas type parameters labeled ‘-’ must only appear in contravariant position. The variance position of a type parameter in a type or template is defined as follows. @@ -467,7 +462,7 @@ The variance position changes at the following constructs. - The variance position of a method parameter is the opposite of the variance position of the enclosing parameter clause. - The variance position of a type parameter is the opposite of the variance position of the enclosing type parameter clause. -- The variance position of the lower bound of a type declaration or type parameter is the opposite of the variance position of the type declaration or parameter. +- The variance position of the lower bound of a type definition or type parameter is the opposite of the variance position of the type definition or parameter. - The type of a mutable variable is always in invariant position. - The right-hand side of a type alias is always in invariant position. - The prefix ´p´ of a type selection `´p.T´` is always in invariant position. @@ -544,13 +539,11 @@ abstract class OutputChannel[-A] { With that annotation, we have that `OutputChannel[AnyRef]` conforms to `OutputChannel[String]`. That is, a channel on which one can write any object can substitute for a channel on which one can write only strings. -## Method Declarations and Definitions +## Method Definitions ```ebnf -Dcl ::= ‘def’ FunDcl -FunDcl ::= FunSig ‘:’ Type Def ::= ‘def’ FunDef -FunDef ::= FunSig [‘:’ Type] ‘=’ Expr +FunDef ::= FunSig [‘:’ Type] [‘=’ Expr] FunSig ::= id [FunTypeParamClause] ParamClauses FunTypeParamClause ::= ‘[’ TypeParam {‘,’ TypeParam} ‘]’ ParamClauses ::= {ParamClause} [[nl] ‘(’ ‘implicit’ Params ‘)’] @@ -562,15 +555,17 @@ ParamType ::= Type | Type ‘*’ ``` -A _method declaration_ has the form `def ´f\,\mathit{psig}´: ´T´`, where ´f´ is the method's name, ´\mathit{psig}´ is its parameter signature and ´T´ is its result type. -A _method definition_ `def ´f\,\mathit{psig}´: ´T´ = ´e´` also includes a _method body_ ´e´, i.e. an expression which defines the method's result. +An _abstract method definition_ has the form `def ´f\,\mathit{psig}´: ´T´`, where ´f´ is the method's name, ´\mathit{psig}´ is its parameter signature and ´T´ is its result type. +A _concrete method definition_ `def ´f\,\mathit{psig}´: ´T´ = ´e´` also includes a _method body_ ´e´, i.e. an expression which defines the method's result. A parameter signature consists of an optional type parameter clause `[´\mathit{tps}\,´]`, followed by zero or more value parameter clauses `(´\mathit{ps}_1´)...(´\mathit{ps}_n´)`. -Such a declaration or definition introduces a value with a (possibly polymorphic) method type whose parameter types and result type are as given. + +If there is no type or term parameter clause, a method definition introduces a method with a proper type, which is also its result type. +Otherwise, it introduces a method with a methodic type whose parameter types and result type are as given. The type of the method body is expected to [conform](06-expressions.html#expression-typing) to the method's declared result type, if one is given. If the method definition is not recursive, the result type may be omitted, in which case it is determined from the packed type of the method body. -A _type parameter clause_ ´\mathit{tps}´ consists of one or more [type declarations](#type-declarations-and-type-aliases), which introduce type parameters, possibly with bounds. +A _type parameter clause_ ´\mathit{tps}´ consists of one or more [type definitions](#type-definitions), which introduce type parameters, possibly with bounds. The scope of a type parameter includes the whole signature, including any of the type parameter bounds as well as the method body, if it is present. A _value parameter clause_ ´\mathit{ps}´ consists of zero or more formal parameter bindings such as `´x´: ´T´` or `´x: T = e´`, which bind value parameters and associate them with their types. @@ -580,11 +575,11 @@ A unary operator is a method named `"unary_´op´"` where ´op´ is one of `+`, ### Default Arguments -Each value parameter declaration may optionally define a default argument. +Each value parameter may optionally define a default argument. The default argument expression ´e´ is type-checked with an expected type ´T'´ obtained by replacing all occurrences of the method's type parameters in ´T´ by the undefined type. -For every parameter ´p_{i,j}´ with a default argument a method named `´f\$´default´\$´n` is generated which computes the default argument expression. -Here, ´n´ denotes the parameter's position in the method declaration. +For every parameter ´p_{i,j}´ with a default argument, a method named `´f\$´default´\$´n` is generated which computes the default argument expression. +Here, ´n´ denotes the parameter's position in the method definition. These methods are parametrized by the type parameter clause `[´\mathit{tps}\,´]` and all value parameter clauses `(´\mathit{ps}_1´)...(´\mathit{ps}_{i-1}´)` preceding ´p_{i,j}´. The `´f\$´default´\$´n` methods are inaccessible for user programs. @@ -595,8 +590,7 @@ In the method def compare[T](a: T = 0)(b: T = a) = (a == b) ``` -the default expression `0` is type-checked with an undefined expected -type. +the default expression `0` is type-checked with an undefined expected type. When applying `compare()`, the default value `0` is inserted and `T` is instantiated to `Int`. The methods computing the default arguments have the form: @@ -638,7 +632,7 @@ That is, the argument is evaluated using _call-by-name_. 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. ###### Example -The declaration +The definition ```scala def whileLoop (cond: => Boolean) (stat: => Unit): Unit diff --git a/docs/_spec/05-classes-and-objects.md b/docs/_spec/05-classes-and-objects.md index 581ba7552104..d777ebfe73e8 100644 --- a/docs/_spec/05-classes-and-objects.md +++ b/docs/_spec/05-classes-and-objects.md @@ -46,8 +46,8 @@ It is forbidden for a template's superclass constructor ´sc´ to be an [enum cl The _least proper supertype_ of a template is the class type or [compound type](03-types.html#compound-types) consisting of all its parent class types. The statement sequence ´\mathit{stats}´ contains member definitions that define new members or overwrite members in the parent classes. -If the template forms part of an abstract class or trait definition, the statement part ´\mathit{stats}´ may also contain declarations of abstract members. -If the template forms part of a concrete class definition, ´\mathit{stats}´ may still contain declarations of abstract type members, but not of abstract term members. +If the template forms part of an abstract class or trait definition, the statement part ´\mathit{stats}´ may also contain definitions of abstract members. +If the template forms part of a concrete class definition, ´\mathit{stats}´ may still contain definitions of abstract type members, but not of abstract term members. Furthermore, ´\mathit{stats}´ may in any case also contain expressions; these are executed in the order they are given as part of the initialization of a template. The sequence of template statements may be prefixed with a formal parameter definition and an arrow, e.g. `´x´ =>`, or `´x´:´T´ =>`. @@ -321,7 +321,7 @@ Modifiers preceding a repeated definition apply to all constituent definitions. The rules governing the validity and meaning of a modifier are as follows. ### `private` -The `private` modifier can be used with any definition or declaration in a template. +The `private` modifier can be used with any definition in a template. Private members of a template can be accessed only from within the directly enclosing template and its companion module or [companion class](#object-definitions). The `private` modifier is also valid for [top-level](09-top-level-definitions.html#packagings) templates. @@ -359,18 +359,17 @@ A different form of qualification is `protected[this]`. A member ´M´ marked with this modifier is called _object-protected_; it can be accessed only from within the object in which it is defined. That is, a selection ´p.M´ is only legal if the prefix is `this` or `´O´.this`, for some class ´O´ enclosing the reference. In addition, the restrictions for unqualified `protected` apply. ### `override` -The `override` modifier applies to class member definitions or declarations. -It is mandatory for member definitions or declarations that override some other concrete member definition in a parent class. -If an `override` modifier is given, there must be at least one overridden member definition or declaration (either concrete or abstract). +The `override` modifier applies to class member definitions. +It is mandatory for member definitions that override some other concrete member definition in a parent class. +If an `override` modifier is given, there must be at least one overridden member definition (either concrete or abstract). ### `abstract override` The `override` modifier has an additional significance when combined with the `abstract` modifier. That modifier combination is only allowed for value members of traits. -We call a member ´M´ of a template _incomplete_ if it is either abstract (i.e. defined by a declaration), or it is labeled `abstract` and `override` and every member overridden by ´M´ is again incomplete. +We call a member ´M´ of a template _incomplete_ if it is either abstract, or it is labeled `abstract` and `override` and every member overridden by ´M´ is again incomplete. Note that the `abstract override` modifier combination does not influence the concept whether a member is concrete or abstract. -A member is _abstract_ if only a declaration is given for it; it is _concrete_ if a full definition is given. ### `abstract` The `abstract` modifier is used in class definitions. @@ -387,7 +386,7 @@ A `final` class member definition may not be 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](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. +Note, however, that [constant value definitions](04-basic-definitions.html#value-definitions) do require an explicit `final` modifier, even if they are defined in a final class or object. `final` is permitted for abstract classes but it may not be applied to traits or incomplete members, and it may not be combined in one modifier list with `sealed`. ### `sealed` @@ -507,15 +506,15 @@ Here, If a class has no formal parameter section that is not implicit, 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](04-basic-declarations-and-definitions.html#variable-declarations-and-definitions) for this parameter is implicitly added to the class. + If a formal parameter definition ´x: T´ is preceded by a `val` or `var` keyword, an accessor [definition](04-basic-definitions.html#value-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´_=`](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 accessor 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´_=`](04-basic-definitions.html#variable-definitions) is also implicitly added to the class. + An 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). + The formal parameter definition 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](04-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-definitions.html#by-name-parameters). - ´t´ is a [template](#templates) of the form @@ -633,7 +632,7 @@ If the case class definition contains an empty value parameter list, the `unappl 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](04-basic-declarations-and-definitions.html#repeated-parameters). +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](04-basic-definitions.html#repeated-parameters). A method named `copy` is implicitly added to every case class unless the class already has a member (directly defined or inherited) with that name, or the class has a repeated parameter. The method is defined as follows: @@ -898,7 +897,7 @@ Such a class ´C´ is conceptually seen as a pair of a Scala class that contains Generally, a _companion module_ of a class is an object which has the same name as the class and is defined in the same scope and compilation unit. Conversely, the class is called the _companion class_ of the module. -Very much like a concrete class definition, an object definition may still contain declarations of abstract type members, but not of abstract term members. +Very much like a concrete class definition, an object definition may still contain definitions of abstract type members, but not of abstract term members. ## Enum Definitions diff --git a/docs/_spec/06-expressions.md b/docs/_spec/06-expressions.md index 78656c4399da..5043e752ebe6 100644 --- a/docs/_spec/06-expressions.md +++ b/docs/_spec/06-expressions.md @@ -91,7 +91,7 @@ This object implements methods in class `scala.AnyRef` as follows: - `eq(´x\,´)` and `==(´x\,´)` return `true` iff the argument ´x´ is also the "null" object. - `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](04-basic-declarations-and-definitions.html#value-declarations-and-definitions) of type ´T´. +- `asInstanceOf[´T\,´]` returns the [default value](04-basic-definitions.html#value-definitions) of type ´T´. - `##` returns ``0``. A reference to any other member of the "null" object causes a `NullPointerException` to be thrown. @@ -106,7 +106,7 @@ SimpleExpr ::= Path 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](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](02-identifiers-names-and-scopes.html#identifiers,-names-and-scopes) at the occurrence of ´x´. +If ´x´ is bound by a definition 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](02-identifiers-names-and-scopes.html#identifiers,-names-and-scopes) at the occurrence of ´x´. 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´. @@ -213,7 +213,7 @@ For this expression to be well-typed, the method must be *applicable* to its arg If ´f´ has a method type `(´p_1´:´T_1, ..., p_n´:´T_n´)´U´`, each argument expression ´e_i´ is typed with the corresponding parameter type ´T_i´ as expected type. Let ´S_i´ be the type of argument ´e_i´ ´(i = 1, ..., n)´. The method ´f´ must be _applicable_ to its arguments ´e_1, ..., e_n´ of types ´S_1, ..., S_n´. -If the last parameter type of ´f´ is [repeated](04-basic-declarations-and-definitions.html#repeated-parameters), [harmonization](#harmonization) is attempted on the suffix ´e_m, ..., e_n´ of the expression list that match the repeated parameter. +If the last parameter type of ´f´ is [repeated](04-basic-definitions.html#repeated-parameters), [harmonization](#harmonization) is attempted on the suffix ´e_m, ..., e_n´ of the expression list that match the repeated parameter. We say that an argument expression ´e_i´ is a _named_ argument if it has the form `´x_i=e'_i´` and `´x_i´` is one of the parameter names `´p_1, ..., p_n´`. Once the types ´S_i´ have been determined, the method ´f´ of the above method type is said to be applicable if all of the following conditions hold: @@ -242,7 +242,7 @@ The behavior of by-name parameters is preserved if the application is transforme In this case, the local value for that parameter has the form `val ´y_i´ = () => ´e´` and the argument passed to the method 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](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). +Such an argument must correspond to a [repeated parameter](04-basic-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 `scala.Seq[´T´]`, for some type ´T´ which conforms to ´S´. In this case, the argument list is transformed by replacing the sequence ´e´ with its elements. When the application uses named arguments, the vararg parameter has to be specified exactly once. @@ -311,7 +311,7 @@ The result of transforming ´f´ is a block of the form where every argument in ´(\mathit{args}\_1), ..., (\mathit{args}\_l)´ is a reference to one of the values ´x_1, ..., x_k´. To integrate the current application into the block, first a value definition using a fresh name ´y_i´ is created for every argument in ´e_1, ..., e_m´, which is initialised to ´e_i´ for 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](04-basic-declarations-and-definitions.html#method-declarations-and-definitions) of this parameter. +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](04-basic-definitions.html#method-definitions) of this parameter. Let ´\mathit{args}´ be a permutation of the generated names ´y_i´ and ´z_i´ such such that the position of each name matches the position of its corresponding parameter in the method type `(´p_1:T_1, ..., p_n:T_n´)´U´`. The final result of the transformation is a block of the form @@ -460,7 +460,7 @@ Block ::= BlockStat {semi BlockStat} [ResultExpr] ``` A _block expression_ `{´s_1´; ...; ´s_n´; ´e\,´}` is constructed from a sequence of block statements ´s_1, ..., s_n´ and a final expression ´e´. -The statement sequence may not contain two definitions or declarations that bind the same name in the same namespace. +The statement sequence may not contain two definitions that bind the same name in the same namespace. The final expression can be omitted, in which case the unit value `()` is assumed. The expected type of the final expression ´e´ is the expected type of the block. @@ -1031,7 +1031,7 @@ The definition of "constant expression" depends on the platform, but they includ - 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, ..., c_n)´`, where all of the ´c_i´'s are themselves constant expressions -- An identifier defined by a [constant value definition](04-basic-declarations-and-definitions.html#value-declarations-and-definitions). +- An identifier defined by a [constant value definition](04-basic-definitions.html#value-definitions). ## Statements @@ -1050,7 +1050,6 @@ TemplateStat ::= Import Statements occur as parts of blocks and templates. A _statement_ can be an import, a definition or an expression, or it can be empty. -Statements used in the template of a class definition can also be declarations. An expression that is used as a statement can have an arbitrary value type. An expression statement ´e´ is evaluated by evaluating ´e´ and discarding the result of the evaluation. @@ -1294,7 +1293,7 @@ Solving means finding a substitution ´\sigma´ of types ´T_i´ for the type pa 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](04-basic-declarations-and-definitions.html#variance-annotations) in the type ´T´ of the expression. +A _maximal_ type ´T_i´ will be chosen if the type parameter ´a_i´ appears [contravariantly](04-basic-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 an _optimal solution_ of the given constraint system for the type ´T´. diff --git a/docs/_spec/07-implicits.md b/docs/_spec/07-implicits.md index 2cd80f227cd4..dacc0c0c277e 100644 --- a/docs/_spec/07-implicits.md +++ b/docs/_spec/07-implicits.md @@ -49,7 +49,7 @@ However, if such a method misses arguments for its implicit parameters, such arg The actual arguments that are eligible to be passed to an implicit parameter of type ´T´ fall into two categories. First, eligible are all identifiers ´x´ that can be accessed at the point of the method call without a prefix and that denote an [implicit definition](#the-implicit-modifier) or an implicit parameter. -To be accessible without a prefix, an identifier must be a local name, a member of an enclosing template or a name introduced by an [import clause](04-basic-declarations-and-definitions.html#import-clauses). +To be accessible without a prefix, an identifier must be a local name, a member of an enclosing template or a name introduced by an [import clause](04-basic-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](05-classes-and-objects.html#object-definitions) of classes that are associated with the implicit parameter's type. diff --git a/docs/_spec/08-pattern-matching.md b/docs/_spec/08-pattern-matching.md index 6646b67ad39e..4a34ae8631c4 100644 --- a/docs/_spec/08-pattern-matching.md +++ b/docs/_spec/08-pattern-matching.md @@ -276,7 +276,7 @@ SimplePattern ::= StableId ‘(’ [Patterns ‘,’] [varid ‘@’] ‘_’ ``` A _pattern sequence_ ´p_1, ..., p_n´ appears in two contexts. -First, in a constructor pattern ´c(q_1, ..., q_m, p_1, ..., 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*`. +First, in a constructor pattern ´c(q_1, ..., q_m, p_1, ..., p_n)´, where ´c´ is a case class which has ´m+1´ primary constructor parameters, ending in a [repeated parameter](04-basic-definitions.html#repeated-parameters) of type `S*`. Second, in an extractor pattern ´x(q_1, ..., q_m, p_1, ..., p_n)´ if the extractor object ´x´ does not have an `unapply` method, but it does define an `unapplySeq` method with a result type that is an extractor type for type `(T_1, ... , T_m, Seq[S])` (if `m = 0`, an extractor type for the type `Seq[S]` is also accepted). The expected type for the patterns ´p_i´ is ´S´. The last pattern in a pattern sequence may be a _sequence wildcard_ `_*`. diff --git a/docs/_spec/11-annotations.md b/docs/_spec/11-annotations.md index 3388d55318ea..3381856d78fb 100644 --- a/docs/_spec/11-annotations.md +++ b/docs/_spec/11-annotations.md @@ -17,8 +17,8 @@ Annotations associate meta-information with definitions. A simple annotation has the form `@´c´` or `@´c(a_1, ..., a_n)´`. Here, ´c´ is a constructor of a class ´C´, which must conform to the class `scala.Annotation`. -Annotations may apply to definitions or declarations, types, or expressions. -An annotation of a definition or declaration appears in front of that definition. +Annotations may apply to definitions, types, or expressions. +An annotation of a definition appears in front of that definition. An annotation of a type appears after that type. An annotation of an expression ´e´ appears after the expression ´e´, separated by a colon. More than one annotation clause may apply to an entity. @@ -86,7 +86,7 @@ def f(x: Option[Int]) = (x: @unchecked) match { ``` Without the `@unchecked` annotation, a Scala compiler could infer that the pattern match is non-exhaustive, and could produce a 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](03-types.html#volatile-types). +* `@uncheckedStable` When applied a value definition, it allows the defined 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: ```scala type A { type T } @@ -97,7 +97,7 @@ val y: x.T // OK since `x' is still a path Without the `@uncheckedStable` annotation, the designator `x` would not be a path since its type `A with B` is volatile. Hence, the reference `x.T` would be malformed. -When applied to value declarations or definitions that have non-volatile types, the annotation has no effect. +When applied to value definitions that have non-volatile types, the annotation has no effect. * `@specialized` When applied to the definition of a type parameter, this annotation causes the compiler to generate specialized definitions for primitive types. An optional list of primitive types may be given, in which case specialization takes into account only those types.