E139: Unknown Named Enclosing Class Or Object
This error occurs when a visibility modifier references a class or object name that cannot be found in the enclosing scope.
In Scala, you can use qualified private or protected modifiers like private[ClassName] or protected[PackageName] to restrict visibility to a specific enclosing class, object, or package. This error is raised when the specified name does not match any enclosing scope.
The class or object named in the visibility modifier was used but could not be resolved. Make sure that the name is not misspelled and refers to an actual enclosing class, object, or package.
Example
class Example:
private[NonExistent] val x: Int = 42
Error
-- [E139] Reference Error: example.scala:2:27 ----------------------------------
2 | private[NonExistent] val x: Int = 42
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| no enclosing class or object is named 'NonExistent'
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| The class or object named 'NonExistent' was used as a visibility
| modifier, but could not be resolved. Make sure that
| 'NonExistent' is not misspelled and has been imported into the
| current scope.
|
-----------------------------------------------------------------------------
Solution
// Use the correct enclosing class name
class Example:
private[Example] val x: Int = 42
// Alternative: Use a simple private modifier
class Example:
private val x: Int = 42
In this article