Why is the case class called "case"?

"Case" is an example of a specific situation; an example of something happening. "

So my question is: why are the Scala 'case' classes called "case"? What's the point? Why is this a β€œcase” and not β€œdata” or something else? What does β€œcase” mean in this case :)

+6
source share
2 answers

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.

+8
source

This is because they can be used to match patterns. The case in English means one of the possibilities; and that which matches the pattern suits you.

for example, "Three cases must be considered here ..."

+1
source

All Articles