Scala 2.13 : wrong number of type parameters for method map

83 views Asked by At

I am newbie to scala and I migrate some code from scala 2.12 to 13. I have the following code

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

I face the following error error:

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)))

Can you explain why scala 2.13 has this problem, I dont find something related to the documentation The map method tries to convert the array and it should be ok

2

There are 2 answers

0
Mateusz Kubuszok On

2.13 was a rewrite of the collections - see https://docs.scala-lang.org/overviews/core/collections-migration-213.html.

In your case in particular the issue is exactly as error says:

  • 2.12 took 2 type parameters in .map
    def map[B, That](f: A => B)(implicit cbf: CanBuildFrom[Array[A], B, That]): That
    
  • 2.13 takes 1 type parameter in .map
    def map[B](f: A => B): Array[B]
    

so in 2.13 it should be

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

For Scala 2.12, the map method added to Array (via an implicit conversion to WrappedArray) has two type parameters, the second of which is used to select a CanBuildFrom instance to constrain the resulting collection type.

Scala 2.13 rewrites the collection APIs (including the Array enrichments) to not use the CanBuildFrom mechanism, therefore the map method (now added via ArraySeq) now only takes a single type parameter, the element type of the resulting ArraySeq. You therefore don't need the Array[StructField] type parameter (and almost certainly don't actually need the StructField type parameter either).

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