Skip to content

fewerBraces: allow partial function arguments without : #17731

@sjrd

Description

@sjrd

It is quite common to pass anonymous partial function to higher-order functions (whether they take a PartialFunction or a regular Function1). In that case, there is no explicit parameter to the lambda, which requires the use of : for the fewerBraces mechanism to kick in:

object Test {
  def main(args: Array[String]): Unit =
    val xs = List((1, "hello"), (true, "bar"), (false, "foo"))
    val ys = xs.collect:
      case (b: Boolean, s) if b => s
    println(ys)
}

I suggest that we infer the : in that situation, so that we could write instead:

object Test {
  def main(args: Array[String]): Unit =
    val xs = List((1, "hello"), (true, "bar"), (false, "foo"))
    val ys = xs.collect // note: no ':' here
      case (b: Boolean, s) if b => s
    println(ys)
}

Currently, the above fails to parse:

-- [E040] Syntax Error: tests/run/hello.scala:5:6 ------------------------------
5 |      case (b: Boolean, s) if b => s
  |      ^^^^
  |      unindent expected, but 'case' found
-- [E040] Syntax Error: tests/run/hello.scala:7:0 ------------------------------
7 |}
  |^
  |'}' expected, but unindent found
-- [E006] Not Found Error: tests/run/hello.scala:6:12 --------------------------
6 |    println(ys)
  |            ^^
  |            Not found: ys
  |
  | longer explanation available when compiling with `-explain`
3 errors found

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions