Description
Compiler version
3.7
Minimized example
def f =
Option("hello").map: s =>
s + ", world"
Output Error/Warning message
-- Error: badfunc.scala:3:2 ----------------------------------------------------
3 | Option("hello").map: s =>
| ^^^^^^^^^^^^^^^^^^^^^^
| not a legal formal parameter for a function literal
-- [E006] Not Found Error: badfunc.scala:4:2 -----------------------------------
4 | s + ", world"
| ^
| Not found: s
|
| longer explanation available when compiling with `-explain`
2 errors found
Why this Error/Warning was not helpful
The "real" error is the missing indent.
The "real" code was a slightly longer expression with another buried error:
allToolArgs.get(ToolName.Target)
.map("." + _).or(Some("")).map: platform =>
JFile(s"${dir.getPath}${platform}.check")
That is, it's not hard for the "real, obvious" error to become obscured.
Suggested improvement
Tell me the "obvious" error. This is not a position for a function literal (perhaps expected type is not a function), so maybe that results in extra help.
Almost any text that patently does not look like a formal parameter may suffer from a missing indent after "colon arrow EOL" (to mean this special syntax for the parameter trailing on the same line as colon).
Maybe it's feasible to re-parse with an indent injected.
I was "amazed" to stumble across:
def f =
Option("hello").map( s =>
println(s"appending")
s + ", world"
)
which works now because of indentation of the lambda body. (In Scala 2, one is perpetually choosing whether to switch from parens to braces in order to add a line of debug.)
But an unindented expression also works:
def f =
Option("hello").map( s =>
s + ", world"
)
Yet it's not sufficient to omit the parens without adding an indent. (Maybe that is a bug. Edit: as specified, since colon token always requires an indent token.) Currently, that is a bit tricky. Normally, x => expr
.