Ad-hoc polymorphism with pattern matching

#1

Reading Programming in Scala 3ed at page 316 et fol. it talks about Typed Patterns, so that you can generalize a method as follows:

def generalSize (x: Any) = x match {
   case s: String => s.length
   case m: Map[_, _] => m.size
   case _ => -1
}

So far, so good. But what if I want to apply the concept to a multivariate function; i.e.;

def generalSize(x: Any, y: Any, z: Any) = ???

?

#2

Depending on your use case, you can either match each argument separately (so you’d have x match {...}, y match { ... } and z match { ... } somewhere in the body), our you can put the values into a tuple and match against it, e.g.

def generalSize (x: Any, y: Any, z: Any) = (x,y,z) match {
     case (s1: String, s2: String, s3: String) => (s1+s2+s3).length
     case (m1: Map[_, _], m2: Map[_,_], m3: Map[_,_]) => m1.size + m2.size + m3.size
     case _ => -1
  } 
#3

The Tuple solution works great.
Thanks