Reputation: 3055
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")
}
Upvotes: 3
Views: 149
Reputation: 9336
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
.
Upvotes: 4