Overriding unapply method

后端 未结 2 730
说谎
说谎 2021-01-12 21:12

I have a case from a library class and I want to override unapply method to reduce the number of parameters I need to pass to

2条回答
  •  无人及你
    2021-01-12 21:23

    I would ditch the overloaded unapply and just use the following for the match:

    a match {
      case MyClass(x, _, _, _) => x  // Result is: 1
      case _ => "no"
    }
    

    EDIT :

    If you really want to avoid the extra underscores, I think you'll need to look at something like:

    a match {
      case x:MyClass => x.a
      case _ => "no"
    }
    

提交回复
热议问题