Back to Scala3

E110: Cyclic Inheritance

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

3.8.41.6 KB
Original Source

E110: Cyclic Inheritance

This error is emitted when a class or trait extends itself, directly or indirectly.

Cyclic inheritance is prohibited in Scala because it would create an impossible situation where a class needs to be fully defined before it can extend itself.


Example

scala
class A extends A

Error

scala
-- [E110] Syntax Error: example.scala:1:16 -------------------------------------
1 |class A extends A
  |                ^
  |                Cyclic inheritance: class A extends itself
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Cyclic inheritance is prohibited in Dotty.
  | Consider the following example:
  |
  | class A extends A
  |
  | The example mentioned above would fail because this type of inheritance hierarchy
  | creates a "cycle" where a not yet defined class A extends itself which makes
  | impossible to instantiate an object of this class
   -----------------------------------------------------------------------------

Solution

scala
// Remove the cyclic inheritance
class A
scala
// Or create a proper inheritance hierarchy
class Base
class A extends Base
<!-- 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>