Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Scala's Extractor - Pattern with Zero variable and Boolean Result

Currently I am learning Scala Extractor and stuck in following confusion. I am unable to understand the following code. In the below pattern match, How UpperCase() return a String while the unapply method is designed to return Boolean?

  object UpperCase {
    def unapply(s: String): Boolean = s.toUpperCase == s
  }

  println(UpperCase.unapply("RAK")) //print boolean true or false.

  "RAK" match{
    case status @ UpperCase() => println("yes - "+ status) //How status holds RAK not boolean value?
    case _ => println("No")
  }
like image 371
Ra Ka Avatar asked Jul 13 '26 01:07

Ra Ka


1 Answers

You are using a boolean extractor, that matches all values v for which x.unapply(v) yields true. The @ is a pattern binder, which binds the variable status to the value matched by the pattern.

In your example, the pattern match the String "RAK", which is bound to the variable status.

like image 166
obourgain Avatar answered Jul 14 '26 15:07

obourgain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!