From Chi Pengfei blog post address: http://blog.csdn.net/cuipengfei1/article/details/33353159
I really can't think of any verbs to take off the clothes, so from now on this series is called Desugar Scala. Unless one day, the idea of a new word:)
Start text.
The methods named Unapply and UNAPPLYSEQ have special meanings in Scala.
As we said before, case class works well with pattern match, and objects with unapply or UNAPPLYSEQ methods have a good application in pattern match, except for case class.
For example, this code:
123 |
object square { def unapply (z: Span class= "kt" >double) : option[ double = some ( math. Sqrt (z} |
We define a unapply method to calculate the square root. We can call it just like a normal method:
12 |
val number: Double = 36.0Square.unapply(number)
|
This will get the square root of 36:6. The actual return value is some (6).
The way above is a waste of unapply, the real benefit of unapply is this:
1 2345 |
val number : double = 36.0 number match { case square (n) => println (s "square root of $number is $n" ) case _ => Println ( "nothing matched" |
So we don't have to explicitly call the Unapply method, but it's used in pattern match and the compiler calls it for us.
When we write the code for this pattern match, the compiler actually does several things for us:
- Call unapply, pass in number
- Receives the return value and determines whether the return value is None or some
- If it is some, untie it and assign the value to n (that is, n in case Square (n))
This code is compiled to look like this:
123456789-ten-19 |
DoubleNumber=36.0D; DoubleD1=Number; OptionLocaloption=Square..MODULE$.Unapply(D1); Call unapply, pass in number BoxedunitLocalboxedunit; If(Localoption.IsEmpty()){Judging the return value is None Predef..MODULE$.println("Nothing Matched"); Localboxedunit=Boxedunit.UNIT; } Else{Judging the return value is some DoubleN=Boxesruntime.Unboxtodouble(Localoption.Get()); Untie the Some and assign the value to n Predef..MODULE$.println(NewStringcontext(Predef..MODULE$.Wraprefarray((Object[])NewString[]{ "Square root of","Is","" })).S (predefmodule$. Genericwraparray (new object[] {< Span class= "line" > boxesruntime. Boxtodouble (numberboxesruntime boxtodouble (n) localboxedunit = boxedunit.< Span class= "NA" >unit; /span>
|
If there is no such combination between the Unapply method and the pattern match syntax, what do we write our own code like?
It may be simpler than the anti-compilation code above, but it is not possible to explicitly call the square method, use if else to determine option, and remove the true return value from the option three things.
The role of UNAPPLYSEQ and unapply is similar, for example:
123456 |
ObjectNames{ DefUnapplyseq (str: string) : option[seq[string = { if str. Contains ( "," some ( str. Split ( "," else none}
|
We define a Unapplyseq method that takes a comma as a delimiter to take apart the string.
Then we can apply it like this:
12345678 |
ValNamesstring="Xiao Ming,xiao hong,tom"NamesstringMatch{ case names (first secondthird) = { println ( "the string Contains three people ' s names ") println (s "$first $second $third" ) } case _ => println ( "nothing matched"
|
This is similar to the example above, but the compiler does more for us here:
- Call Unapplyseq, pass in namesstring
- Receives the return value and determines whether the return value is None or some
- If it's some, untie it.
- Determine if the number of elements in the sequence that were obtained after unlocking is three
- If it is three, remove the three elements and assign the values to First,second and third
Without this combination of the Unapplyseq method and the pattern match syntax, it would be tedious to write our own code to do these five things.
Scala Learning notes-Extractor unapply