scala - 如何重组代码以避免警告:“通过创建2元组来适应参数列表”

原文 标签 scala apache-spark

How to restructure code to avoid warning: “Adapting argument list by creating a 2-tuple”

The following code has already been verified as working as I want it: i.e. adding a tuple to an existing sequence (also of tuples)

iter.map { r =>     // Iterator of org.apache.spark.sql.Row
   r.toSeq :+ (outColName,locMap(r.getAs[String](inColName)))
}

However our build system has fail on warning enabled and thus errors out the above code with:

[info] 'compiler-interface' not yet compiled for Scala 2.10.6. Compiling...
[info]   Compilation completed in 24.504 s
[error] /home/../Sampling.scala:40: Adapting argument list 
         by creating a 2-tuple: this may not be what you want.
[error]         signature: SeqLike.:+[B >: A, That](elem: B)(implicit bf: scala.collection.generic.CanBuildFrom[Repr,B,That]): That
[error]   given arguments: outColName, locMap(r.getAs[String](inColName))
[error]  after adaptation: SeqLike.:+((outColName, locMap(r.getAs[String](inColName))): (String, Int))
[error]           r.toSeq :+ (outColName,locMap(r.getAs[String](inColName)))
[error]                   ^
[error] one error found

Now - as mentioned - this is what I want. But Travis needs to be made happy. So what is the correct invocation to signfify the same -i.e. this is the desired behavior - and avoid the warnings here?

Answer

Try to help the compiler by explicitly adding the method invocation and the parenthesis.

iter.map { r: Row => 
   r.toSeq.:+( (outColName,locMap(r.getAs[String](inColName))):Any )
}

翻译

以下代码已经按照我的要求进行了验证:即将一个元组添加到现有序列中(也包括元组)

iter.map { r =>     // Iterator of org.apache.spark.sql.Row
   r.toSeq :+ (outColName,locMap(r.getAs[String](inColName)))
}


但是,我们的构建系统已启用fail on warning,因此,使用以下代码会出错:

[info] 'compiler-interface' not yet compiled for Scala 2.10.6. Compiling...
[info]   Compilation completed in 24.504 s
[error] /home/../Sampling.scala:40: Adapting argument list 
         by creating a 2-tuple: this may not be what you want.
[error]         signature: SeqLike.:+[B >: A, That](elem: B)(implicit bf: scala.collection.generic.CanBuildFrom[Repr,B,That]): That
[error]   given arguments: outColName, locMap(r.getAs[String](inColName))
[error]  after adaptation: SeqLike.:+((outColName, locMap(r.getAs[String](inColName))): (String, Int))
[error]           r.toSeq :+ (outColName,locMap(r.getAs[String](inColName)))
[error]                   ^
[error] one error found


现在-如上所述-这就是我想要的。但是Travis需要变得快乐。那么什么是正确的调用来表示相同的-即这是理想的行为-并在此避免警告?
最佳答案
尝试通过显式添加方法调用和括号来帮助编译器。

iter.map { r: Row => 
   r.toSeq.:+( (outColName,locMap(r.getAs[String](inColName))):Any )
}
相关推荐

scala - 如何在Spark中为不同的文件名调用单独的逻辑

scala - Akka.Kafka-警告消息-恢复分区

eclipse - 安装Mac OS X Sierra(Mac OS 10.12)后,Eclipse scala-ide无法启动

scala - 计算Spark中UDF的调用

scala - 火花一次输出到kafka

scala - 如何为nd4j和deeplearning4j设置Scala SBT项目

scala - 从scala的类型获取ParameterizedType?

scala - ADT类型类实例的泛型派生

scala - Scala akka-http WebSocket:如何保存客户端连接,并在需要时向客户端推送消息?

java - 实施Google云存储的最佳方法是什么?