Skip to content

Undeterministic eta expansion for a method with default arguments #19266

Open
@matwojcik

Description

@matwojcik

Compiler version

3.3.1

Minimized code

Code from the gist:

  def fn1(x: Int, y: Int = 5)(z: Int) = Some(x+y+z)
  def fn2(x: Int)(y: Int) = Some(x+y)

  def program = 
    fn1(4) // why this compiles
    fn2(2) // when this does not
    5
scala-cli compile Unused.scala -Wnonunit-statement

or

scala-cli compile https://gist.github.com/matwojcik/85c0527502871b0a672441728601afc9 -Wnonunit-statement

Output

[error] missing argument list for value of type Int => Some[Int]
[error]     fn2(2) // when this does not

Expectation

I would expect that either:

  • both fn1 and fn2 calls fail with missing argument
  • both fn1 and fn2 calls warn with unused value of type Int => Some[Int] (add : Unit to discard silently)

If you adapt this code to scala 2.13 (add object - see https://gist.github.com/matwojcik/55e9e27c9581f136dcd3266d1b4a48ab) and run

scala-cli compile --scala 2.13 -Xsource:3 [Unused.scala](https://gist.github.com/matwojcik/55e9e27c9581f136dcd3266d1b4a48ab) -Wnonunit-statement

then the result is:

[warn] ./Unused213.scala:8:5
[warn] unused value of type Int => Some[Int] (add `: Unit` to discard silently)
[warn]     fn2(2) // when this does not
[warn]     ^^^^^^
[warn] ./Unused213.scala:7:5
[warn] unused value of type Int => Some[Int] (add `: Unit` to discard silently)
[warn]     fn1(4) // why this compiles
[warn]     ^^^^^^

So something that I expected in the first place - warning about non used value. Scala3 though is apparently doing eta expansion not in the deterministic way here - when the function is having a default parameters.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions