Back to Scala3

E177: Constructor Proxy Shadows

docs/_docs/reference/error-codes/E177.md

3.8.43.0 KB
Original Source

E177: Constructor Proxy Shadows

This error occurs when a reference to a constructor proxy for an inner class shadows an outer reference with the same name, such as a method or object.

In Scala 3, constructor proxies are automatically generated for classes, allowing you to create instances without writing new. However, when an inner class shares a name with an outer definition (method, object, or value), the compiler cannot determine which one you intended to use.


Longer explanation:

There is an ambiguity in the meaning of the call

scala
MyClass(...)

It could mean creating an instance of an inner class with

scala
new MyClass(...)

Or it could mean calling an outer method/object with the same name:

scala
MyClass(...)

To disambiguate, use an explicit new if you mean the former, or use a full prefix if you mean the latter.


Example

scala
object Test:
  def MyClass(s: String): String = s

  class Outer:
    class MyClass(s: String)
    val x = MyClass("hello")  // ambiguous: inner class or outer method?

Error

scala
-- [E177] Reference Error: example.scala:6:12 ----------------------------------
6 |    val x = MyClass("hello")  // ambiguous: inner class or outer method?
  |            ^^^^^^^
  |           Reference to constructor proxy for class MyClass in class Outer
  |           shadows outer reference to method MyClass in object Test
  |
  |           The instance needs to be created with an explicit `new`.
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | There is an ambiguity in the meaning of the call
  |
  |    MyClass(...)
  |
  | It could mean creating an instance of class MyClass in class Outer with
  |
  |    new MyClass(...)
  |
  | Or it could mean calling method MyClass in object Test as in
  |
  |    MyClass(...)
  |
  | To disambiguate, use an explicit `new` if you mean the former,
  | or use a full prefix for MyClass if you mean the latter.
   -----------------------------------------------------------------------------

Solution

Use explicit new if you want to create an instance of the inner class:

scala
object Test:
  def MyClass(s: String): String = s

  class Outer:
    class MyClass(s: String)
    val x = new MyClass("hello")  // explicitly create inner class instance

Or use a full prefix to call the outer method:

scala
object Test:
  def MyClass(s: String): String = s

  class Outer:
    class MyClass(s: String)
    val x = Test.MyClass("hello")  // explicitly call outer method
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>