Suppose the following scenario
sealed trait Status
case object Active extends Status
case object Inactive extends Status
@scala.deprecated("deprecated because reasons")
case object Disabled extends Status /
Considering Disabled object cannot be removed and given val status: Status = getStatus there is either one of this problems:
- Fails with match not exhaustive:
status match {
case Active => ???
case Inactive => ???
}
- Fails with deprecated value being used
status match {
case Active => ???
case Inactive => ???
case Disabled => ???
}
- Losing compile time safety
status match {
case Active => ???
case Inactive => ???
case _ => ???
}
Can type-safety exhaustive match be achieved in this scenario?
CodePudding user response:
I think option 2 is the way to go. But to make it work you have to disable the warning selectively. This is supported starting with Scala 2.13.2 and 2.12.13
@annotation.nowarn("cat=deprecation")
def someMethod(s: Status) = s match {
case Active => "Active"
case Inactive => "Inactive"
case Disabled => "Disabled"
}
CodePudding user response:
There may be some confusion here between "deprecated" and "unavailable".
The Disabled value is deprecated but it is still available for use. And since it is still available, it must still be supported in the code because it can still be used. So option 2 is the only acceptable solution because it is the only one that can processes Disabled appropriately with type safety. Option 1 does not process Disabled and option 3 loses type safety.
Also note that none of these code samples "fail", they just generate warnings. So the solution is to suppress the deprecation warnings as described in the other answer.
