-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
10 changed files
with
119 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
missing-param-type-tuple.scala:3: error: missing parameter type | ||
Note: The expected type requires a one-argument function accepting a 2-Tuple. | ||
Consider a pattern matching anoynmous function, `{ case (a, b) => ... }` | ||
val x: ((Int, Int)) => Int = (a, b) => 0 | ||
^ | ||
missing-param-type-tuple.scala:3: error: missing parameter type | ||
val x: ((Int, Int)) => Int = (a, b) => 0 | ||
^ | ||
missing-param-type-tuple.scala:5: error: missing parameter type | ||
Note: The expected type requires a one-argument function accepting a 3-Tuple. | ||
Consider a pattern matching anoynmous function, `{ case (param1, ..., param3) => ... }` | ||
val y: ((Int, Int, Int)) => Int = (a, b, !!) => 0 | ||
^ | ||
missing-param-type-tuple.scala:5: error: missing parameter type | ||
val y: ((Int, Int, Int)) => Int = (a, b, !!) => 0 | ||
^ | ||
missing-param-type-tuple.scala:5: error: missing parameter type | ||
val y: ((Int, Int, Int)) => Int = (a, b, !!) => 0 | ||
^ | ||
missing-param-type-tuple.scala:7: error: missing parameter type | ||
Note: The expected type requires a one-argument function accepting a 3-Tuple. | ||
Consider a pattern matching anoynmous function, `{ case (param1, ..., param3) => ... }` | ||
val z: ((Int, Int, Int)) => Int = (a, NotAVariablePatternName, c) => 0 | ||
^ | ||
missing-param-type-tuple.scala:7: error: missing parameter type | ||
val z: ((Int, Int, Int)) => Int = (a, NotAVariablePatternName, c) => 0 | ||
^ | ||
missing-param-type-tuple.scala:7: error: missing parameter type | ||
val z: ((Int, Int, Int)) => Int = (a, NotAVariablePatternName, c) => 0 | ||
^ | ||
8 errors found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class C { | ||
|
||
val x: ((Int, Int)) => Int = (a, b) => 0 | ||
|
||
val y: ((Int, Int, Int)) => Int = (a, b, !!) => 0 | ||
|
||
val z: ((Int, Int, Int)) => Int = (a, NotAVariablePatternName, c) => 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
not-a-legal-formal-parameter-tuple.scala:2: error: not a legal formal parameter. | ||
Note: Tuples cannot be directly destructured in method or function parameters. | ||
Either create a single parameter accepting the Tuple2, | ||
or consider a pattern matching anonymous function: `{ case (a, b) => ... } | ||
val x: ((Int, Int) => Int) = (((a, b)) => a) | ||
^ | ||
not-a-legal-formal-parameter-tuple.scala:3: error: not a legal formal parameter. | ||
Note: Tuples cannot be directly destructured in method or function parameters. | ||
Either create a single parameter accepting the Tuple2, | ||
or consider a pattern matching anonymous function: `{ case (param1, param2) => ... } | ||
val y: ((Int, Int, Int) => Int) = (((a, !!)) => a) | ||
^ | ||
not-a-legal-formal-parameter-tuple.scala:4: error: not a legal formal parameter. | ||
Note: Tuples cannot be directly destructured in method or function parameters. | ||
Either create a single parameter accepting the Tuple3, | ||
or consider a pattern matching anonymous function: `{ case (param1, ..., param3) => ... } | ||
val z: ((Int, Int, Int) => Int) = (((a, NotAPatternVariableName, c)) => a) | ||
^ | ||
three errors found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class C { | ||
val x: ((Int, Int) => Int) = (((a, b)) => a) | ||
val y: ((Int, Int, Int) => Int) = (((a, !!)) => a) | ||
val z: ((Int, Int, Int) => Int) = (((a, NotAPatternVariableName, c)) => a) | ||
} |