Skip to content

class default parameter values have access to companion object members while it should be a compilation error #20045

Open
@unkarjedy

Description

@unkarjedy

Scala 3.3.1

class MyClass(int: Int = fooInt) {  //no error, expected "Not found: fooInt"
  println(fooInt) //error: "Not found: fooInt" (as expected)
}

object MyClass {
  def fooInt: Int = ???
}

The code should produce an error at the first line as fooInt is not in the scope.
The correct way to use it is to import MyClass.fooInt or use qualified name.

The same issue is for case classes and ordinary classes.

I guess it's a leaked implementation detail of "universal apply" feature + a bug in handling apply methods with default parameter values.
Now even non-case classes have synthetic apply methods.
It seams during compilation the default arguments are simply moved to the apply method of companion object (potentially synthetic), where object members are accessible.

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