9

Is there way to partially match a tuple without having to specify the size? For example, if I had a tuple

val v = ( "Dr", "John","H", "Watson") 

I'd like to be able to do something like :

v match { 
   case ( "Dr", : _*) => "What's up, Doc?"
   case ( "Mr", name,   :_*) =>  s"Welcome, Mr. ${name}"
   case _ => "Have we met?"
} 

This doesn't compile, :_* normally means an undetermined number of parameters, but can't be used in this case apparently. The idea would be to be able to use this matcher for any tuple bigger than 2. I know i can do it converting v to a List (for example) first, just want to know if it's possible to do it with a tuple.

EDIT: the most information I found on the web is this discussion, which dates back to scala 2.8, so I'm gonna with the 'No, you can't' answer.

4

3 回答 3

11

Tuples are structures for heterogeneous types. As such, they implement the productIterator trait, so you could do:

v.productIterator.toList match { 
  case "Dr" :: _ => "What's up, Doc?"
  case "Mr" :: name :: _ =>  s"Welcome, Mr. ${name}"
  case _ => "Have we met?"
}

But your example really looks like you want a Seq[String] straight away. Is there any reason for wishing to use tuples?

于 2013-03-29T23:17:44.197 回答
10

This can be done using shapeless's conversions from tuples to HLists,

scala> import shapeless._
import shapeless._

scala> import Tuples._
import Tuples._

scala> val v = ( "Dr", "John","H", "Watson")
v: (String, String, String, String) = (Dr,John,H,Watson)

scala> v.hlisted match {
     |   case "Dr" :: _ => "What's up Doc?"
     |   case "Mr" :: name :: _ => s"Welcome, Mr. ${name}"
     |   case _ => "Have we met?"
     | }
res0: String = What's up Doc?

Although it's not visible in the above example, note that where possible full static type information is maintained for the names bound in the case clauses, for example,

scala> (23, "foo", true).hlisted match {
     |   case i :: s :: b :: HNil => if(b) i+s.length else 0
     |   case _ => -1
     | }
res1: Int = 26
于 2013-03-30T10:51:48.227 回答
-1

Try this:

case tuple:Product => {
    tuple.productIterator.toList match {
        case (head::rest) => ...

    }
}
于 2017-02-12T12:32:38.480 回答