I recently came across the following code pattern in Scala:
sealed trait ConfirmAction
object ConfirmAction {
final case object NewGame extends ConfirmAction
final case object Quit extends ConfirmAction
}
I understand that this defines subtypes of the ConfirmAction sealed trait as case objects within the companion object. However, I'm curious about a couple of things:
Why are the case objects NewGame and Quit marked as final? Isn't this redundant since case objects are inherently singletons and can't be extended?
Is there a specific reason for choosing this pattern of defining subtypes within the companion object, rather than declaring them directly under the sealed trait itself, like:
sealed trait ConfirmAction
final case object NewGame extends ConfirmAction
final case object Quit extends ConfirmAction
Additionally, I'd like to know more about this approach and its benefits. Can you please explain the rationale behind these choices and point me to resources where I can learn more about this way of declaring subtyping patterns?
Thank you!
looking around and googling for this specific implementation of the domain model in google.