E096: Class And Companion Name Clash
This error is emitted when both a class/trait and its companion object define a class, trait, or object with the same name.
A class and its companion object share the same namespace for nested types, so they cannot both define a type with the same name.
Example
class Outer:
class Inner
object Outer:
class Inner
Error
-- [E096] Naming Error: example.scala:2:8 --------------------------------------
2 | class Inner
| ^
| Name clash: both class Outer and its companion object defines Inner
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| A class and its companion object cannot both define a class, trait or object with the same name:
| - class Outer defines class Inner
| - object Outer defines class Inner
-----------------------------------------------------------------------------
Solution
// Use different names for the nested types
class Outer:
class InnerClass
object Outer:
class InnerObject
// Or define the type in only one place
class Outer:
class Inner
object Outer:
// Use Outer#Inner or define something else here
def create(): Outer#Inner =
val out = new Outer()
new out.Inner()
In this article