Scala-dev: Deprecate method value (`foo _`) syntax?

Created on 23 Nov 2016  路  7Comments  路  Source: scala/scala-dev

the difference between foo _ and foo(_) (or foo(_, _) and so on) has always been a puzzler factory.

if the choice is between foo _ and foo(_), then losing the first option isn't bad, but if the choice is between foo _ and foo(_, _, _, _, _, _) then it starts feeling different.

but in the wild, perhaps most uses of foo _ would also compile simply as foo because a function type is already expected?

personally, I think I tend to write foo _ even when just foo would compile, just to explicitly signal to the reader that foo is definitely not being called.

Most helpful comment

For 0 argument the only alternative is () => foo(). Especially now that automatic eta-expansion of 0-arg methods is deprecated.

All 7 comments

For 0 argument the only alternative is () => foo(). Especially now that automatic eta-expansion of 0-arg methods is deprecated.

True, but I think the benefit of

  • eta-expanding more uniformly (except for 0-ary methods, but regardless of the expected type), and
  • removing method value syntax

outweighs having to explicitly eta-expand for the 0-ary case.

this is being taken up at https://github.com/lampepfl/dotty/issues/2570

foo(_, _, _, _, _, _) will never be necessary because just foo will always work

@adriaanm Or stop parenthesis injection for nullary functions.

Dotty's doing that, but I don't think we can make everyone insert the needed () in their code base in 2.13.

Carried over from #489:

Currently, http://www.scala-lang.org/files/archive/spec/2.12/06-expressions.html#method-values says:

The expression e _ is well-formed if e is of method type or if e is a call-by-name parameter. [...]
If e is a parameterless method or call-by-name parameter of type => T, e _ represents the function of type () => T, which evaluates e when it is applied to the empty parameter list ().

Since we've removed eta-expansion for nullary methods, for consistency we should also deprecate and drop the last part re: "parameterless method or call-by-name parameter". Those will need to be written explicitly using () => e.

Eventually (ideally), all method references will be eta-expanded regardless of expected type, and method value syntax will be dropped since it's redundant.

this is happening in Dotty eventually, but out of scope for Scala 2

Was this page helpful?
0 / 5 - 0 ratings