You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/_spec/05-classes-and-objects.md
+1-52Lines changed: 1 addition & 52 deletions
Original file line number
Diff line number
Diff line change
@@ -295,57 +295,6 @@ It is a static error if the inheritance closure of a class type consists of an i
295
295
296
296
[^kennedy]: Kennedy, Pierce. [On Decidability of Nominal Subtyping with Variance.](https://research.microsoft.com/pubs/64041/fool2007.pdf) in FOOL 2007
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
-
traitGreeting {
333
-
valname:String
334
-
valmsg="How are you, "+name
335
-
}
336
-
classCextends {
337
-
valname="Bob"
338
-
} withGreeting {
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
-
349
298
## Modifiers
350
299
351
300
```ebnf
@@ -592,7 +541,7 @@ A constructor expression is either a self constructor invocation `this(´\mathit
592
541
The self constructor invocation must construct a generic instance of the class.
593
542
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.
594
543
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.
596
545
The rest of the constructor expression is type-checked and evaluated as a method body in the current class.
597
546
598
547
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