The main use of the case keyword in other languages ββis in switch stats, which are mainly used on enum , or comparable values, such as int or string , used to represent various well-defined cases:
switch (value) { case 1: // do x - case 2: // do y - default: // optional }
In scala, these classes often represent concrete, well-defined possible instances of an abstract class and are used in much the same way that imperative code uses switch-statements in match -clauses:
value match { case Expr(lhs, rhs) => // do x case Atomic(a) => // do y case _ => // optional, but will throw exception if something cannot be matched whereas switch won't }
A significant part of the behavior of classes of cases, such as the method of constructing them, is aimed at facilitating / using them in such statements.
midor source share