Skip to content

"method apply must be called with () argument" must be generated for scala.Function0 #21351

Open
@unkarjedy

Description

@unkarjedy

Compiler version

3.3.3

Minimized code

object Example:
  //Scala 2 library method (scala.Function0)
  val f: Function0[Int] = () => 42
  f.apply // <==== Why no error here?
  f.apply()

  //Scala 3 library method
  scala.Tuple.apply
  scala.Tuple.apply()
  scala.runtime.Scala3RunTime.assertFailed
  scala.runtime.Scala3RunTime.assertFailed()

  // User code
  class A {
    def apply(): Int = 42
  }
  val a = new A()
  a.apply
  a.apply()

Expectation

Compiler should generate error "method apply in class Function0 must be called with () argument" for f.apply similar to all other method calls without ()

See https://docs.scala-lang.org/scala3/reference/changed-features/eta-expansion-spec.html

If m is has an empty argument list (i.e. has type ()R):
If the expected type is of the form () => T, we eta expand.
If m is defined by Java, or overrides a Java defined method, we insert ().
Otherwise we issue an error of the form: method must be called with () argument

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions