Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An object with unapply working in middle of a case statement

Tags:

scala

scala> object Test {
         def unapply[L,R](v: (L, R)) = Some(v)
       }
defined object Test

scala> (1, 2) match {
         case 1 Test 2 => println("First")
         case Test((1, 2)) => println("Second")
         case _ => println("Third")
       }
First

Could somebody explain why is the first case working with the object in between the two values of the tuple?

like image 453
ptrlaszlo Avatar asked Jan 16 '17 20:01

ptrlaszlo


People also ask

What is a case object in Java?

A Case Object is also like an object, which has more attributes than a regular Object. It is a blend of both case classes and object. A case object has some more features than a regular object. It is serializable. It has a by default hashCode implementation.

What is a case object in Scala?

} This is a common way of using the Scala object construct. A case object is like an object, but just like a case class has more features than a regular class, a case object has more features than a regular object. Its features include:

What are case objects used for?

Because of these features, case objects are primarily used in two places (instead of regular objects): When creating containers for “messages” that you want to pass between other objects (such as with the Akka actors library)

What is case statement?

Case Statement in JavaScript Introduction to Case Statement in JavaScript JavaScript is the most popular client-side scripting language which is supported by almost all browsers. It is an open-source dynamic programming language that front end developers use.


2 Answers

From the Scala Specifications 8.1.10:

Infix Operation Patterns
An infix operation pattern p;op;q is a shorthand for the constructor or extractor pattern op(p,q).

So

case 1 Test 2 => println("First")

is rewritten to

case Test(1, 2) => println("First")

You can actually see this in the REPL by adding //print at the end of the line and pressing <tab> twice:

scala> (1, 2) match { case 1 Test 2 => println("First") } //print
   scala.Tuple2.apply[Int, Int](1, 2) match {
     case $line10.$read.$iw.$iw.Test(1, 2) => scala.Predef.println("First")
   } // : Unit
like image 53
Marth Avatar answered Nov 15 '22 05:11

Marth


Infix operation patterns - see The Neophyte's Guide to Scala Part 1: Extractors:

If you followed the Scala course at Coursera, you learned that you can destructure lists and streams in a way that is akin to one of the ways you can create them, using the cons operator, :: or #::, respectively:

val xs = 58 #:: 43 #:: 93 #:: Stream.empty
xs match {
  case first #:: second #:: _ => first - second
  case _ => -1
}

Maybe you have wondered why that is possible. The answer is that as an alternative to the extractor pattern notation we have seen so far, Scala also allows extractors to be used in an infix notation. So, instead of writing e(p1, p2), where e is the extractor and p1 and p2 are the parameters to be extracted from a given data structure, it’s always possible to write p1 e p2

... our PremiumUser extractor could also be used in a pattern that reads name PremiumUser score. However, this is not something you would do in practice. Usage of infix operation patterns is only recommended for extractors that indeed are supposed to read like operators, which is true for the cons operators of List and Stream, but certainly not for our PremiumUser extractor.

like image 29
Suma Avatar answered Nov 15 '22 03:11

Suma