【发布时间】:2021-12-18 15:10:07
【问题描述】:
我试图了解 Match 类型中的多种类型。即使下面的作品
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
}
但即使匹配类型工作正常,我也无法按照样式工作
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>
【问题讨论】:
标签: scala scala-3 match-types