I have two case class like:
case class B(value:Int)
case class A(a:String, b:B*) extends ALike
and I want to do pattern match on an instance of A:
def foo(al:ALike) = {
al match {
case A(a, bs) => ...
}
}
Scalac doesn't understand that bs is a Seq[B] and thinks that it is just one B. Why is that the case and how should I do pattern match on it?
It's a varargs argument, so you need to explain that to the compiler explicitly. Use the following case expression:
def foo(al:ALike) = {
al match {
case A(a, bs @ _*) => ...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With