Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate trailing _ to force eta expansion #18867

Open
nicolasstucki opened this issue Nov 7, 2023 · 5 comments
Open

Deprecate trailing _ to force eta expansion #18867

nicolasstucki opened this issue Nov 7, 2023 · 5 comments

Comments

@nicolasstucki
Copy link
Contributor

nicolasstucki commented Nov 7, 2023

Warn in: 3.4
Error in: TBD

@nicolasstucki nicolasstucki self-assigned this Nov 10, 2023
@nicolasstucki
Copy link
Contributor Author

nicolasstucki commented Nov 13, 2023

Example:

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

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Nov 14, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Nov 15, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Nov 15, 2023
@nicolasstucki nicolasstucki removed their assignment Jan 10, 2024
@ByteEater-pl
Copy link

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

@nicolasstucki
Copy link
Contributor Author

Do you mean this

val f = foo

instead of this?

val f = x => foo(x)

@som-snytt
Copy link
Contributor

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.

@joroKr21
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants