Back to Scala3

E173: Cannot Be Accessed

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

3.8.41.4 KB
Original Source

E173: Cannot Be Accessed

This error occurs when trying to access a member that is not visible due to access modifiers (private, protected, etc.) from the current scope.

Scala's access modifiers control which code can access which members. Private members can only be accessed from within the defining class, while protected members can be accessed from subclasses.


Example

scala
class Secret {
  private def hidden = 42
}

def test = {
  val s = new Secret
  s.hidden
}

Error

scala
-- [E173] Reference Error: example.scala:7:4 -----------------------------------
7 |  s.hidden
  |  ^^^^^^^^
  |method hidden cannot be accessed as a member of (s : Secret) from the top-level definitions in package <empty>.
  |  private method hidden can only be accessed from class Secret.

Solution

scala
class Secret {
  private def hidden = 42

  // Expose via a public method
  def revealed: Int = hidden
}

def test = {
  val s = new Secret
  s.revealed
}
scala
// Or change the access modifier
class Secret {
  def visible = 42
}

def test = {
  val s = new Secret
  s.visible
}
<!-- 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>