Skip to content

Commit f552c88

Browse files
committed
Apply early-initializers.md
1 parent ce10384 commit f552c88

File tree

2 files changed

+1
-52
lines changed

2 files changed

+1
-52
lines changed

docs/_spec/05-classes-and-objects.md

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -295,57 +295,6 @@ It is a static error if the inheritance closure of a class type consists of an i
295295

296296
[^kennedy]: Kennedy, Pierce. [On Decidability of Nominal Subtyping with Variance.]( https://research.microsoft.com/pubs/64041/fool2007.pdf) in FOOL 2007
297297

298-
### Early Definitions
299-
300-
```ebnf
301-
EarlyDefs ::= ‘{’ [EarlyDef {semi EarlyDef}] ‘}’ ‘with’
302-
EarlyDef ::= {Annotation} {Modifier} PatVarDef
303-
```
304-
305-
A template may start with an _early field definition_ clause, which serves to define certain field values before the supertype constructor is called.
306-
In a template
307-
308-
```scala
309-
{ val ´p_1´: ´T_1´ = ´e_1´
310-
...
311-
val ´p_n´: ´T_n´ = ´e_n´
312-
} with ´sc´ with ´mt_1´ with ´mt_n´ { ´\mathit{stats}´ }
313-
```
314-
315-
The initial pattern definitions of ´p_1 , \ldots , p_n´ are called _early definitions_.
316-
They define fields which form part of the template.
317-
Every early definition must define at least one variable.
318-
319-
An early definition is type-checked and evaluated in the scope which is in effect just before the template being defined, augmented by any type parameters of the enclosing class and by any early definitions preceding the one being defined.
320-
In particular, any reference to `this` in an early definition refers to the identity of `this` just outside the template.
321-
Consequently, it is impossible for an early definition to refer to the object being constructed by the template, or to refer to one of its fields and methods, except for any other preceding early definition in the same section.
322-
Furthermore, references to preceding early definitions always refer to the value that's defined there and do not take into account overriding definitions.
323-
In other words, a block of early definitions is evaluated exactly as if it were a local block containing a number of value definitions.
324-
325-
Early definitions are evaluated before the superclass constructor of the template is called, in the order they are defined.
326-
327-
###### Example
328-
Early definitions are particularly useful for traits, which do not have normal constructor parameters.
329-
Example:
330-
331-
```scala
332-
trait Greeting {
333-
val name: String
334-
val msg = "How are you, "+name
335-
}
336-
class C extends {
337-
val name = "Bob"
338-
} with Greeting {
339-
println(msg)
340-
}
341-
```
342-
343-
In the code above, the field `name` is initialized before the constructor of `Greeting` is called.
344-
Therefore, field `msg` in class `Greeting` is properly initialized to `"How are you, Bob"`.
345-
346-
If `name` had been initialized instead in `C`'s normal class body, it would be initialized after the constructor of `Greeting`.
347-
In that case, `msg` would be initialized to `"How are you, <null>"`.
348-
349298
## Modifiers
350299

351300
```ebnf
@@ -592,7 +541,7 @@ A constructor expression is either a self constructor invocation `this(´\mathit
592541
The self constructor invocation must construct a generic instance of the class.
593542
I.e. if the class in question has name ´C´ and type parameters `[´\mathit{tps}\,´]`, then a self constructor invocation must generate an instance of `´C´[´\mathit{tps}\,´]`; it is not permitted to instantiate formal type parameters.
594543

595-
The signature and the self constructor invocation of a constructor definition are type-checked and evaluated in the scope which is in effect at the point of the enclosing class definition, augmented by any type parameters of the enclosing class and by any [early definitions](#early-definitions) of the enclosing template.
544+
The signature and the self constructor invocation of a constructor definition are type-checked and evaluated in the scope which is in effect at the point of the enclosing class definition, augmented by any type parameters of the enclosing class.
596545
The rest of the constructor expression is type-checked and evaluated as a method body in the current class.
597546

598547
If there are auxiliary constructors of a class ´C´, they form together with ´C´'s primary [constructor](#class-definitions) an overloaded constructor definition.

0 commit comments

Comments
 (0)