VOOZH about

URL: https://www.geeksforgeeks.org/scala/scala-unapplyseq-method/

⇱ Scala | unapplySeq() method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Scala | unapplySeq() method

Last Updated : 8 Apr, 2019
The unapplySeq() method is an Extractor method. It extracts an Object of particular type and then again reconstructs it into a Sequence of extracted values and the length of this Sequence is not specified at the time of compilation. So, in order to reconstruct an Object that contains a Sequence, you need to utilize this unapplySeq method. Syntax:
def unapplySeq(object: X): Option[Seq[T]]
Here, we have an object of type X and this method either returns None, when the object does not match or returns a Sequence of extracted values of type T, enclosed in class Some. Now, lets understand it through some examples. Example :
Output:
The age of Nidhi is: 24
The age of Nisha is: 22
Here, we have used a trait Product2 in the Option in order to pass two arguments to it. Product2 is a Cartesian product of two elements. Example :
Output:
List(1, 3, 5)
Here, we have used a function sortWith, that sorts the stated sequence as specified by the comparison function.
Comment
Article Tags:

Explore