diff options
author | Sebastian Harko <sebastian.harko@lightbend.com> | 2016-10-16 02:19:33 -0700 |
---|---|---|
committer | Sebastian Harko <sebastian.harko@lightbend.com> | 2016-10-16 02:19:33 -0700 |
commit | c44a63e2574e2eca2b8f089920071f1d80aa7add (patch) | |
tree | e59c8c6edc5aa303e239ad3a3f5809092bc86cf1 | |
parent | 5ebd55243ccc5ca637aa8c32f085a3ffb0fd93cb (diff) | |
download | dotty-c44a63e2574e2eca2b8f089920071f1d80aa7add.tar.gz dotty-c44a63e2574e2eca2b8f089920071f1d80aa7add.tar.bz2 dotty-c44a63e2574e2eca2b8f089920071f1d80aa7add.zip |
explanation for error message related to use of early definitions
-rw-r--r-- | src/dotty/tools/dotc/parsing/Parsers.scala | 2 | ||||
-rw-r--r-- | src/dotty/tools/dotc/reporting/diagnostic/messages.scala | 44 |
2 files changed, 45 insertions, 1 deletions
diff --git a/src/dotty/tools/dotc/parsing/Parsers.scala b/src/dotty/tools/dotc/parsing/Parsers.scala index 507a2e80c..46e7512f3 100644 --- a/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/src/dotty/tools/dotc/parsing/Parsers.scala @@ -2061,7 +2061,7 @@ object Parsers { def templateBody(): (ValDef, List[Tree]) = { val r = inDefScopeBraces { templateStatSeq() } if (in.token == WITH) { - syntaxError("early definitions are not supported; use trait parameters instead") + syntaxError(EarlyDefinitionsNotSupported()) in.nextToken() template(emptyConstructor) } 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 + } } |