File tree 2 files changed +45
-1
lines changed
2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -2061,7 +2061,7 @@ object Parsers {
2061
2061
def templateBody (): (ValDef , List [Tree ]) = {
2062
2062
val r = inDefScopeBraces { templateStatSeq() }
2063
2063
if (in.token == WITH ) {
2064
- syntaxError(" early definitions are not supported; use trait parameters instead " )
2064
+ syntaxError(EarlyDefinitionsNotSupported () )
2065
2065
in.nextToken()
2066
2066
template(emptyConstructor)
2067
2067
}
Original file line number Diff line number Diff line change @@ -274,4 +274,48 @@ object messages {
274
274
275
275
val explanation = " "
276
276
}
277
+
278
+ case class EarlyDefinitionsNotSupported ()(implicit ctx: Context ) extends Message (9 ) {
279
+ val kind = " Syntax"
280
+
281
+ val msg = " early definitions are not supported; use trait parameters instead"
282
+
283
+ val code1 =
284
+ """ |trait Logging {
285
+ | val f: File
286
+ | f.open()
287
+ | onExit(f.close())
288
+ | def log(msg: String) = f.write(msg)
289
+ |}
290
+ |
291
+ |class B extends Logging {
292
+ | val f = new File("log.data") // triggers a null pointer exception
293
+ |}
294
+ |
295
+ |class C extends {
296
+ | val f = new File("log.data") // early definition gets around the null pointer exception
297
+ |} with Logging""" .stripMargin
298
+
299
+ val code2 =
300
+ """ |trait Logging(f: File) {
301
+ | f.open()
302
+ | onExit(f.close())
303
+ | def log(msg: String) = f.write(msg)
304
+ |}
305
+ |
306
+ |class C extends Logging(new File("log.data"))""" .stripMargin
307
+
308
+ val explanation =
309
+ hl """ Earlier versions of Scala did not support trait parameters and "early definitions" (also known as "early initializers")
310
+ |were used as an alternative.
311
+ |
312
+ |Example of old syntax:
313
+ |
314
+ | $code1
315
+ |
316
+ |The above code can now be written as:
317
+ |
318
+ | $code2
319
+ | """ .stripMargin
320
+ }
277
321
}
You can’t perform that action at this time.
0 commit comments