score:4
looking at this question from the point of view of the scala language, the implementation works as specification requires. see http://www.scala-lang.org/docu/files/scalareference.pdf
in §5.3.2, case classes are defined to include an implementation of unapply in the companion (extractor) object.
however, when we get to pattern matching (§8.1), case classes have their own section on matching, §8.1.6, which specifies their behaviour in pattern matching based on the parameters to the constructor, without any reference to the already-generated unapply/unapplyseq:
8.1.6 constructor patterns
syntax:
simplepattern ::= stableid ‘(’ [patterns] ‘)
a constructor pattern is of the form c(p1,…,pn) where n≥0. it consists of a stable identifier c, followed by element patterns p1,…,pn. the constructor c is a simple or qualified name which denotes a case class. if the case class is monomorphic, then it must conform to the expected type of the pattern, and the formal parameter types of x's primary constructor are taken as the expected types of the element patterns p1,…,pn. if the case class is polymorphic, then its type parameters are instantiated so that the instantiation of c conforms to the expected type of the pattern. the instantiated formal parameter types of c's primary constructor are then taken as the expected types of the component patterns p1,…,pn. the pattern matches all objects created from constructor invocations c(v1,…,vn) where each element pattern pi matches the corresponding value vi.
the document continues to describe the use of unapply/unapplyseq in §8.1.8; but this is a separate, disjoint, part of the specification, which is applied for classes which are not case classes.
so you can consider unapply to be a useful method to use in your own code, but not something that is required by pattern matching inside the scala language.
score:2
my guess is that this is an optimisation scalac performs. the unapply
method is synthetic, so the compiler knows its implementation and might improve on the runtime performance.
if that theory is correct, the following should be different:
object cat {
def unapply(c: cat): option[string] = some(c.name)
}
class cat(val name: string) extends animal
Source: stackoverflow.com
Related Query
- unapply method of a case class is not used by the Scala compiler to do pattern matching, why is that?
- Scala compiler says my method is recursive in case when implicits and anonymous class is used
- Why does the Scala compiler say that copy is not a member of my case class?
- Scala case class is not matched in receive method (in akka actors)
- Scala compiler error due to constructor parameter (property) having same name in both base and derived class and used in derived method
- Alternate constructor on Scala case class not defined: not enough arguments for method
- Declaring a method to accept an unknown case class as argument to use it in pattern matching in Scala
- How to make the scala compiler find case classes used with wrong arguments
- Scala 2.9.2 can not handle the overloaded format method of the String class - why?
- Scala - not a case class nor does it have method .unapply
- Parse json array to a case class in scala using playframework with the fields in json not matching the fields in case class
- How to write a toCSV method for scala case class that would generate the csv string for the class?
- Error in running Scala Program: Main method not found in class main, please define the main method
- Why Scala case class copy method parameterised only with the variables defined in the case class?
- Getting an error in intellij referencing a scala method I'm not actually using in a class I have used
- Scala case class object 'key not found' when used as a HashMap key in Spark
- How to get around the Scala case class limit of 22 fields?
- Why won't the Scala compiler apply tail call optimization unless a method is final?
- Scala case class private constructor but public apply method
- Scala 2.10 reflection, how do I extract the field values from a case class, i.e. field list from case class
- If the Nothing type is at the bottom of the class hierarchy, why can I not call any conceivable method on it?
- Why not make every Scala class a case class?
- Explain the `LowPriorityImplicits` pattern used in Scala type-level programming
- What is the performance impact of using the type class pattern in Scala
- Obtaining the full info about fields of the case class in Scala
- Scala wont pattern match with java.lang.String and Case Class
- Custom Json Writes with combinators - not all the fields of the case class are needed
- How do I create an explicit companion object for a case class which behaves identically to the replaced compiler provided implicit companion object?
- Why does Scala warn about type erasure in the first case but not the second?
- Scala spark: how to use dataset for a case class with the schema has snake_case?
More Query from same tag
- Lift - Returning Errors and Updating Parent Div Class From Ajax Form
- How to unwrap Option[MyClassName]?
- Scala 2.11 override things form an abstract class
- Send a POST request in scala using cURL command to slack
- Read concatenated json objects in Spark?
- Higher-kinded types—why possible Scala but not F#?
- inserting a list of objects not working in slick
- Scala first program - error
- Scala - How to create a label / button that shows more options when clicked
- Parallel recursion in scala
- org.apache.spark.ml.regression.LinearRegression: fit, train, and predict
- getFromDirectory not working in a akka-http route
- Scala: who can explain this?
- Why does Map.mapValues change the answer
- How to redefine a key in SBT?
- Adding a custom logic to a Scala's constructor
- Some List specs2 matcher
- IntelliJ IDEA Scala plugin good code red
- Why does the Scala Actor implementation involve synchronized code?
- how to collect data after window operation? GroupBy messes up the order and I get incorrect results
- Setting up multiple domains with Play Framework
- Which design pattern is my Scala application using?
- What is the fp-style way of doing "naive hashmap"
- Returning extra data as part of return type
- shapeless filter a list of options
- How to pass a tuple argument the best way?
- found String, Required (String,String,String,Int): tuples-scala
- Is it considered a good style to use whitespace to align the code?
- passing parameter from an angular table to another one
- How to filter in rdd containing array of tuples?