2

Assume that I have a code like:

val pf: PartialFunction[String, Unit] =
  "string" match { case regex(g1, g2, _*) =>
    function(g1, g2)
  }

pf has methods isDefinedAt and apply. Will the regex search be evaluated once, in isDefinedAt point, or the job will be done twice?

If once, how args g1, g2 get passed to apply method?

dveim
  • 3,381
  • 2
  • 21
  • 31

1 Answers1

1

The regex will be evaluated once. The work is done in the extractor, Regex.unapplySeq. The extractor's return type is Option[List[String]] so it knows whether the regex was a match (either Some or None) and the captured groups (the List[String]) all in one bundle.

dwickern
  • 3,519
  • 1
  • 14
  • 21
  • But how captured groups appear in apply method? So, the code above should be translated in new PartialFunction { def apply ... ; def isDefinedAt ... }. Could you please show how would these methods look like? – dveim Dec 23 '15 at 07:44