简体   繁体   English

Scala:在映射中发现类型不匹配

[英]Scala: type mismatch found in mapping

Error: type mismatch; 错误:类型不匹配; found : List[List[(Char, Int)]] required: List[(Char, Int)] at q<- x 找到:List [List [(Char,Int)]]必需:List [(Char,Int)]在q <-x

a2 reduceLeft ((x,y)=>  
  for{  
    q<- x  
    b<- y  
  } yield (q::b::Nil)  
) 

where, a2 : List[List[(Char, Int)]]. 其中,a2:List [List [(Char,Int)]]。

If a2 is List[List[(Char, Int)]] , x is List[(Char, Int)] , so q is (Char, Int) , and so is b, how it found List[List[(Char, Int)]] ? 如果a2是List[List[(Char, Int)]] ,x是List[(Char, Int)] ,则q是(Char, Int) ,b也是如此,它是如何找到List[List[(Char, Int)]]吗?

The problem is that yield returns a List[(Char, Int)] so the for loop returns a List[List[(Char, Int)]] . 问题在于yield返回一个List[(Char, Int)]因此for循环会返回一个List[List[(Char, Int)]]

Therefore, the reduceLeft complains since it expects the return type to be List[(Char, Int)] . 因此, reduceLeft抱怨,因为它期望返回类型为List[(Char, Int)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM