scala3
scala3 copied to clipboard
Deprecate trailing `_` to force eta expansion
Warn in: 3.4 Error in: TBD
Example:
def foo(x: Int): Int = x
val f = foo _
Can it be done concisely in another way not requiring minting a variable name?
Do you mean this
val f = foo
instead of this?
val f = x => foo(x)
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.
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
- https://github.com/scala/scala3/issues/23281