Scala 模式类型与预期类型不兼容

Scala 模式类型与预期类型不兼容,scala,Scala,我已经阅读了其他相关问题,但还没有得到答案 代码: DoubleType图案类型与预期类型不兼容。找到DoubleType.type。必需:(数据类型,字符串) 我尝试了两个简化版本,语法看起来不错 List(1,2,3).zip(List(4,5,6)).map { case(a, b) => a match {case 1 => println(s"First is $a, second is $b") case _ =>

我已经阅读了其他相关问题,但还没有得到答案

代码:

DoubleType
图案类型与预期类型不兼容。找到DoubleType.type。必需:(数据类型,字符串)

我尝试了两个简化版本,语法看起来不错

List(1,2,3).zip(List(4,5,6)).map { case(a, b) => 
        a match {case 1 => println(s"First is $a, second is $b") 
                 case _ => println("test")}}
以下方法也适用

inputType.zipWithIndex.map {
  case (inputType, idx) =>
    inputType match {
      case DoubleType => println("test1")
      case _ => println('test2 ')
    }
}
我不明白为什么在添加了
zip
之后,会出现这种模式匹配类型错误。

您没有将
inputType
inputColName
分组为
tuple2

inputType.zip(inputColName).zipWithIndex.map {
  case ((inputType, inputColName), idx) =>
    inputType match {
      case DoubleType => println("test1")
      case _ => println("test2")
    }
}
当您使用
zip
作为

inputType.zip(inputColName)
然后Scala编译器会将其视为

List[(org.apache.spark.sql.types.NumericType with Product with Serializable, String)]
当您添加
.zipWithIndex
时,Scala编译器会将其读取为

List[((org.apache.spark.sql.types.NumericType with Product with Serializable, String), Int)]
问题

当您将case定义为
case(inputType,inputColName,idx)
时,Scala编译器会将
inputType
视为
(org.apache.spark.sql.types.NumericType with Product with Serializable,String)
inputColName
视为
Int
((org.apache.spark.sql.types.NumericType with Product with Serializable,String),Int)
创建
inputType.zip(inputColName).zipWithIndex时形成的数据类型
。因此,
idx
永远不会被识别

即使您在不使用
idx的情况下执行以下操作,那么它的有效性也同样有效(现在
案例的
inputType
被视为
(org.apache.spark.sql.types.NumericType with Product with Serializable,String)

我希望解释清楚。

您没有将
inputType
inputColName
分组为
tuple2

inputType.zip(inputColName).zipWithIndex.map {
  case ((inputType, inputColName), idx) =>
    inputType match {
      case DoubleType => println("test1")
      case _ => println("test2")
    }
}
当您使用
zip
作为

inputType.zip(inputColName)
然后Scala编译器会将其视为

List[(org.apache.spark.sql.types.NumericType with Product with Serializable, String)]
当您添加
.zipWithIndex
时,Scala编译器会将其读取为

List[((org.apache.spark.sql.types.NumericType with Product with Serializable, String), Int)]
问题

当您将case定义为
case(inputType,inputColName,idx)
时,Scala编译器会将
inputType
视为
(org.apache.spark.sql.types.NumericType with Product with Serializable,String)
inputColName
视为
Int
((org.apache.spark.sql.types.NumericType with Product with Serializable,String),Int)
创建
inputType.zip(inputColName).zipWithIndex时形成的数据类型
。因此,
idx
永远不会被识别

即使您在不使用
idx的情况下执行以下操作,那么它的有效性也同样有效(现在
案例的
inputType
被视为
(org.apache.spark.sql.types.NumericType with Product with Serializable,String)


我希望解释清楚。

太棒了!如何打印数据类型?是scala REPL。我只是复制粘贴。:)太棒了!如何打印数据类型?是scala REPL。我只是复制粘贴。:)