Search code examples
scalapattern-matchingcase-class

Matching only some parameters in case classes and not put N place holder for all options


I have my matching:

 val product = parser next match {
      case EvElemStart(_, "Product", attrs, _) =>
        Some(parseProduct( parser, attrs ))
      case _ =>
        readNext()
 }

Here I have 4 possible attributes and use 2nd and 3th one - "Product" and attrs.

Let's imagine that EvelementStart has 20 parameters.

Then, should I mention "_" place holder 19 times if I want use/check/match only first, or only second value/parameter?


Solution

  • Let's imagine that EvelementStart has 20 parameters.

    Then, should I mention "_" place holder 19 times if I want use/check/match only first, or only second value/parameter?

    Yes that is the way it works. You have some alternatives though:

    You could just match on the type, and use the fields of the matched object:

    case e : EvElemStart if  (e.typeString == "Product") =>
        Some(parseProduct( parser, e.attrs ))
    

    Or you could write your own extractor: http://www.scala-lang.org/node/112