Richard Searle

home

Scala curiosity using multiple inheritance

27 Jul 2015

A new project requires a large number of scodec Codecs and the associated domain model. A sealed trait with derived class objects and classes is a good match for much of the problem.

A simple transcription of the ICD lead to the following code:

sealed trait Warning
case object Closed extends Warning
case object Warn extends Warning
  
sealed trait Failure
case object Closed extends Failure
case object Failed extends Failure

Which is of course illegal since Closed appears twice. (The terminology is weird but matches the source documentation)

Making Closed implement both Traits works fine, although it does look a little strange.

sealed trait Warning
case object Warn extends Warning
  
sealed trait Failure
case object Failed extends Failure
  
case object Closed extends Failure with Warning

Arguably Failure and Warning are now coupled to some extent, although I cannot see any adverse consequences. In any event, that would be irrelevant in this context.