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。我只是复制粘贴。:)