Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the reasoning behind adding the "case" keyword to Scala?

Apart from:

case class A

... case which is quite useful?

Why do we need to use case in match? Wouldn't:

x match {
  y if y > 0 => y * 2
  _ => -1
}

... be much prettier and concise?

Or why do we need to use case when a function takes a tuple? Say, we have:

val z = List((1, -1), (2, -2), (3, -3)).zipWithIndex

Now, isn't:

z map { case ((a, b), i) => a + b + i }

... way uglier than just:

z map (((a, b), i) => a + b + i)

...?

like image 368
Michal Rus Avatar asked Oct 19 '13 22:10

Michal Rus


People also ask

What does case do in Scala?

What is Scala Case Class? A Scala Case Class is like a regular class, except it is good for modeling immutable data. It also serves useful in pattern matching, such a class has a default apply() method which handles object construction. A scala case class also has all vals, which means they are immutable.

What does case _ mean in Scala?

case _ => does not check for the type, so it would match anything (similar to default in Java). case _ : ByteType matches only an instance of ByteType . It is the same like case x : ByteType , just without binding the casted matched object to a name x . – Gregor Raýman.

What are the benefits of using case class in Scala?

It has a by default hashCode implementation. The one of the topmost benefit of Case Class is that Scala Compiler affix a method with the name of the class having identical number of parameters as defined in the class definition, because of that you can create objects of the Case Class even in the absence of the keyword new.

What is the return keyword in Scala?

Other computer languages commonly use the keyword return to return the output of a method. In Scala, the last statement of a context is its return value. Bonus: What is a context in Scala ? # This section is aimed at those with proficiency in other computer languages.

What are the Scala features that support functional programming?

Another Scala feature that provides support for functional programming is the case class. A case class has all of the functionality of a regular class, and more. When the compiler sees the case keyword in front of a class, it generates code for you, with the following benefits:

How to modify fields defined on case class in Scala?

As you have learned from the Scala Programming Features tutorial, Scala favours the use of immutability. As a results, fields defined on case class are immutable by default and as such you cannot modify them.


1 Answers

First, as we know, it is possible to put several statements for the same case scenario without needing some separation notation, just a line jump, like :

x match {
       case y if y > 0 => y * 2
                          println("test")
                          println("test2")  // these 3 statements belong to the same "case"
}

If case was not needed, compiler would have to find a way to know when a line is concerned by the next case scenario.

For example:

x match {
   y if y > 0 => y * 2
   _ => -1
}

How compiler would know whether _ => -1 belongs to the first case scenario or represents the next case?

Moreover, how compiler would know that the => sign doesn't represent a literal function but the actual code for the current case?

Compiler would certainly need a kind of code like this allowing cases isolation: (using curly braces, or anything else)

x match {
    {y if y > 0 => y * 2}
    {_ => -1}  // confusing with literal function notation
}

And surely, solution (provided currently by scala) using case keyword is a lot more readable and understandable than putting some way of separation like curly braces in my example.

like image 168
Mik378 Avatar answered Nov 13 '22 16:11

Mik378