E112: Unable to Extend Sealed Class
This error is emitted when attempting to extend a sealed class or trait from a different source file.
Sealed classes and traits can only be extended in the same source file where they are declared. This restriction enables exhaustive pattern matching.
Example
sealed trait Animal
case class Dog(name: String) extends Animal
case class Cat(name: String) extends Animal
Error
-- [E112] Syntax Error: example.scala:1:32
1 |case class Cat(name: String) extends Animal
| ^^^^^^
| Cannot extend sealed trait Animal in a different source file
Solution
// Define all subclasses in the same file as the sealed trait
sealed trait Animal
case class Dog(name: String) extends Animal
case class Cat(name: String) extends Animal
// Or use an open or abstract class instead of sealed
abstract class Animal
case class Dog(name: String) extends Animal
// Cat can now be defined in another file
In this article