Scala | unapplySeq() method
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.
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.
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.
List(1, 3, 5)
Here, we have used a function sortWith, that sorts the stated sequence as specified by the comparison function.
Please Login to comment...