diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2013-11-28 20:16:17 +0100 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2013-12-01 23:23:01 +0100 |
commit | e571c9cc3ee5a9e96b899285cdd2df3cdce06898 (patch) | |
tree | 7118c1dc5cf23dac0495d3c3e334be621de98916 /src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala | |
parent | 073ebbd20ce9775260b83a78ecf9ed6a3e6d3d9e (diff) | |
download | scala-e571c9cc3ee5a9e96b899285cdd2df3cdce06898.tar.gz scala-e571c9cc3ee5a9e96b899285cdd2df3cdce06898.tar.bz2 scala-e571c9cc3ee5a9e96b899285cdd2df3cdce06898.zip |
Better error messages for common Function/Tuple mistakes
Firstly, for `((a, b) => c): (Tuple2[A, B] => C)`, we currently
just offer "missing parameter type." Is something of a rite of
passage to know that you need `{ case (...)}`
This commit stops short DWIM, but does offer a diagnostic to guide
the user towards the supported way of destructuring a `Tuple` in
the sole argument of a `Function1`.
Secondly, another (less common?) way one might try to write a function
to destructure a single tuple argument is:
(((a, b)) => c)
The parser now matches offers a specific error message for this, and
points out the alternatives.
In both cases, we avoid offering syntactically invalid alternatives,
by detecting names that aren't valid as variable-patterns, and
falling back to generic "paramN" in the error message.
A handly utility function to sequence a list of options is liberated
from the pattern matcher for broader use.
Diffstat (limited to 'src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala')
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala b/src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala index 695a1e2e24..b801b644fb 100644 --- a/src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala +++ b/src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala @@ -109,6 +109,22 @@ trait TypeDiagnostics { case x => x.toString } + /** + * [a, b, c] => "(a, b, c)" + * [a, B] => "(param1, param2)" + * [a, B, c] => "(param1, ..., param2)" + */ + final def exampleTuplePattern(names: List[Name]): String = { + val arity = names.length + val varPatterNames: Option[List[String]] = sequence(names map { + case name if nme.isVariableName(name) => Some(name.decode) + case _ => None + }) + def parenthesize(a: String) = s"($a)" + def genericParams = (Seq("param1") ++ (if (arity > 2) Seq("...") else Nil) ++ Seq(s"param$arity")) + parenthesize(varPatterNames.getOrElse(genericParams).mkString(", ")) + } + def alternatives(tree: Tree): List[Type] = tree.tpe match { case OverloadedType(pre, alternatives) => alternatives map pre.memberType case _ => Nil |