Syntax of 0-ary functions

As I understand the way the syntax of zero-ary functions is handled in Scala has changed (or is changing). Can someone help me understand what the rules and gotchas are?

I’m happy that the collective opinion seems to be to syntactically distinguish between a function and an application of a function. But I’m happy without completely understanding the implications.

For example x.toString does not require any parens. And neither do x.toSeq or x.toSet.
But if I define an operator such as the following, the definition and call-site both seem to require parens, well in 2.13 they seem to.

  def ?():Rte = Or(this,EmptyWord)
  def *():Rte = Star(this) // postfix operator
  def +():Rte = Cat(this,Star(this)) // postfix operator

If you declare a method with an empty parameter list, you will get a warning if you apply this without the parameter list. (And according to the warning message, Scala 3 will parse this strictly as a function reference.) But I don’t think this warning has been introduced only recently.

You don’t get a warning in this specific case, because compiler and IDE have specific rules for what they consider Java-defined accessor methods.

These don’t have a parameter list to start with (as they’re supposed to be pure).

As for non-alphanumeric “postfix operators” without param list: In order to distinguish from the case where the colon is part of the operator name, you’ll have to separate the “return type colon” by space:

def ? : String = "x"
?
3 Likes