From c44a63e2574e2eca2b8f089920071f1d80aa7add Mon Sep 17 00:00:00 2001 From: Sebastian Harko Date: Sun, 16 Oct 2016 02:19:33 -0700 Subject: explanation for error message related to use of early definitions --- .../tools/dotc/reporting/diagnostic/messages.scala | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src/dotty/tools/dotc/reporting/diagnostic/messages.scala') diff --git a/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 9cfac4801..9a02e0b04 100644 --- a/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -274,4 +274,48 @@ object messages { val explanation = "" } + + case class EarlyDefinitionsNotSupported()(implicit ctx:Context) extends Message(9) { + val kind = "Syntax" + + val msg = "early definitions are not supported; use trait parameters instead" + + val code1 = + """|trait Logging { + | val f: File + | f.open() + | onExit(f.close()) + | def log(msg: String) = f.write(msg) + |} + | + |class B extends Logging { + | val f = new File("log.data") // triggers a null pointer exception + |} + | + |class C extends { + | val f = new File("log.data") // early definition gets around the null pointer exception + |} with Logging""".stripMargin + + val code2 = + """|trait Logging(f: File) { + | f.open() + | onExit(f.close()) + | def log(msg: String) = f.write(msg) + |} + | + |class C extends Logging(new File("log.data"))""".stripMargin + + val explanation = + hl"""Earlier versions of Scala did not support trait parameters and "early definitions" (also known as "early initializers") + |were used as an alternative. + | + |Example of old syntax: + | + |$code1 + | + |The above code can now be written as: + | + |$code2 + |""".stripMargin + } } -- cgit v1.2.3