Back to Scala3

E087: Duplicate Private Protected Qualifier

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

3.8.41.5 KB
Original Source

E087: Duplicate Private Protected Qualifier

This error is emitted when both private and protected modifiers with scope qualifiers (like [ClassName]) are used on the same definition.

It is not allowed to combine private and protected modifiers even if they are qualified to different scopes. A member can only have one visibility modifier.


Example

scala
class Example:
  private[Example] protected[Example] def method: Int = 42

Error

scala
-- [E087] Syntax Error: example.scala:2:28 -------------------------------------
2 |  private[Example] protected[Example] def method: Int = 42
  |                            ^
  |                            Duplicate private/protected modifier
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | It is not allowed to combine `private` and `protected` modifiers even if they are qualified to different scopes
   -----------------------------------------------------------------------------

Solution

scala
// Use only one access modifier
class Example:
  protected[Example] def method: Int = 42
<!-- 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>