I am trying to understand multiple types in Match type. Even though below works
type C2[N, M] = (N,M) match {
case (String, Int) => Int
case (String, String) => Int
case (Int, String) => Int
}
def fn[M,N](x:N,y:M):C2[N,M] = (x,y) match {
case xx: (String, Int) @unchecked => xx(0).size + xx(1)
case xx: (String, String) @unchecked => xx(0).size + xx(1).size
case xx: (Int, String) @unchecked => xx(0) + xx(1).size
}
But i am unable to work below style eventhough Match type works OK
scala> type C2[N, M] = N match {
| case String => M match {
| case Int => Int
| }
| }
scala> def fn[M,N](x:N,y:M):C2[x.type,y.type] = x match {
| case xx:String => y match {
| case yy:Int => (xx.size + yy)
| }
| }
-- Error:
3 | case yy:Int => (xx.size + yy)
| ^^^^^^^^^^^^
| Found: Int
| Required: (y : M) match {
| case Int => Int
| }
scala>
I got it working as following
It looks if you want to create a match type with multiple type parameters you need to make each case another match type.