scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Deprecate trailing `_` to force eta expansion

Open nicolasstucki opened this issue 2 years ago • 6 comments

Warn in: 3.4 Error in: TBD

nicolasstucki avatar Nov 07 '23 07:11 nicolasstucki

Example:

def foo(x: Int): Int = x
val f = foo _

nicolasstucki avatar Nov 13 '23 15:11 nicolasstucki

Can it be done concisely in another way not requiring minting a variable name?

ByteEater-pl avatar Mar 02 '24 03:03 ByteEater-pl

Do you mean this

val f = foo

instead of this?

val f = x => foo(x)

nicolasstucki avatar Mar 02 '24 08:03 nicolasstucki

scala> def f(x: Int): Int = x
def f(x: Int): Int

scala> locally { f; 42 }
-- [E178] Type Error: --------------------------------------------------------------------------------------------------
1 |locally { f; 42 }
  |          ^
  |          missing argument list for value of type Int => Int
  |
  | longer explanation available when compiling with `-explain`
1 error found

scala> locally { val _ = f; 42 }
val res0: Int = 42

scala> locally { f _; 42 }
-- [E178] Type Error: --------------------------------------------------------------------------------------------------
1 |locally { f _; 42 }
  |          ^
  |          missing argument list for value of type Int => Int
  |
  | longer explanation available when compiling with `-explain`
1 error found

I was going to joke that "the underscore has to go somewhere", but explicit trailing underscore also errors.

som-snytt avatar Mar 02 '24 09:03 som-snytt

We should make up our mind 😄 - the warning is wrong about varargs:

Welcome to Scala 3.5.1-RC1-bin-SNAPSHOT-nonbootstrapped-git-952b928 (17.0.9, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> def foo(xs: Int*) = xs.length
def foo(xs: Int*): Int

scala> val bar = foo _
1 warning found
-- Warning: --------------------------------------------------------------------
1 |val bar = foo _
  |          ^^^^^
  |          The syntax `<function> _` is no longer supported;
  |          you can simply leave out the trailing ` _`
val bar: Seq[Int] => Int = Lambda$12572/0x000000e802c9dc10@2173b693

scala> val bar = foo
-- [E178] Type Error: ----------------------------------------------------------
1 |val bar = foo
  |          ^^^
  |          missing argument list for method foo
  |
  |            def foo(xs: Int*): Int
  |
  | longer explanation available when compiling with `-explain`
1 error found

joroKr21 avatar May 27 '24 06:05 joroKr21

  • https://github.com/scala/scala3/issues/23281

xuwei-k avatar May 29 '25 04:05 xuwei-k