Suppose I define errors for my Scala application. I want them to be Error
, Warning
, and Ok
. I would like the Error
and Warning
to contain a human-readable text message.
I would like also to assign numerical codes (0 - for Ok
, 1 - for Warning
, and 2 - for Error
) to find the most serious error in an errors list for example.
So, I define this error stuff as follows:
object MyErrors { abstract sealed case class MyError(code: Int, maybeMessage: Option[String]) object Ok extends MyError(0, None) final case class Warning(message) extends MyError(1, Some(message)) final case class Error(message) extends MyError(2, Some(message)) }
Does it make sense? How would you implement it?
(2 * Warning == Error)
? :) If not, the concept of arithmetic data types is a bit overspecific. Maybe a rough grouping like in logging (severe, critical, fatal, ...) is better, and can just be modeled by inheritance. Also: The classEither
might be useful for you. – user unknownx
. – NicolasOrdering
. – Michael