Symbolic methods break with : whitespace style

In Scala 3 you can call higher order methods without braces

numbers.map: n =>
  n * 2

This is nice, but fails when the method is named with a symbol.

extension [A](a: A)
  inline def |>[B](inline f: A => B): B = f(a)

10 |>(n => n + 5)
// ✅

10 |>: n =>
  n + 5 
// ❌ not a legal formal parameter for a function literal

10 |> : n =>
  n + 5 
// ❌ even fails with the extra space.

10 `|>`: n =>
  n + 5
// ✅ backticks work, but no one wants to do that.

Scala interprets : as part of the identifier name, |>: rather than |>.

I want to be consistent in my preference for whitespace style in my codebase, and pipe is a scenario where a symbolic operator is worthwhile. Scala supports whitespace, and Scala supports symbolic operators. Can we do anything to improve their unison in this edge case?

2 Likes

This was intentional.

You get a colon token only after alphanumeric identifiers or backticked identifiers and closing brackets.

Sorry I don’t know what the underlying motivation is, whether it is clarity or disambiguation.

This syntax is currently deprecated:

class ** :
  def z = 42

The example for argument to an operator got backticks in the PR that settled these rules.

Edit: I think there is speculation for indented regions for infix. The use case was complex boolean expressions, where you have to use parens instead of just indenting a || b. Maybe this is another motivating use case.

1 Like