Scala 2.13:方法Map的类型参数数量错误

s6fujrry  于 6个月前  发布在  Scala
关注(0)|答案(2)|浏览(78)

我是scala的新手,我将一些代码从scala 2.12迁移到13。

override def transformSchema(schema: StructType): StructType = {
    StructType(schema.fields ++ this.getOutputCols.map[StructField, Array[StructField]]( col => StructField(col, StringType)))
  }

字符串
出现以下错误:

wrong number of type parameters for method map: [B](f: String => B)(implicit ct: scala.reflect.ClassTag[B]): Array[B]
[ERROR]     StructType(schema.fields ++ this.getOutputCols.map[StructField, Array[StructField]]( col => StructField(col, StringType)))


你能解释一下为什么scala 2.13会有这个问题吗?我没有找到与文档相关的东西。map方法试图转换数组,应该没问题。

e3bfsja2

e3bfsja21#

2.13是对集合的重写-参见https://docs.scala-lang.org/overviews/core/collections-migration-213.html
在你的例子中,问题就像error说的那样:

  • 2.12在.map
def map[B, That](f: A => B)(implicit cbf: CanBuildFrom[Array[A], B, That]): That

中取了2个类型参数

  • 2.13在.map
def map[B](f: A => B): Array[B]

中接受1个类型参数

所以2.13应该是

StructType(schema.fields ++ getOutputCols.map[StructField](col => StructField(col, StringType)))

w51jfk4q

w51jfk4q2#

对于Scala 2.12,添加到Arraymap方法(通过隐式转换为WrappedArray)有两个类型参数,其中第二个用于选择CanBuildFrom示例以约束结果集合类型。
Scala 2.13重写了集合API(包括Array增强)不使用CanBuildFrom机制,因此使用map方法(现在通过ArraySeq添加)现在只接受一个类型参数,结果ArraySeq的元素类型。因此,您不需要Array[StructField]类型参数(几乎可以肯定,实际上也不需要StructField类型参数)。

this.getOutputCols.map(col => StructField(col, StringType)).toArray

字符串

相关问题