本文介绍了重命名导入的静态函数有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下Scala代码:
Consider the following Scala code:
object MainObject {
def main(args: Array[String]) {
import Integer.{
parseInt => atoi
}
println(atoi("5")+2);
println((args map atoi).foldLeft(0)(_ + _));
}
第一个println工作正常,输出7,但第二个,尝试映射函数atoi的字符串数组不起作用,错误值atoi不是对象java.lang.Integer的成员
First println works fine and outputs 7, but the second one, attempting to map array of strings against a function atoi doesn't work,with error "value atoi is not a member of object java.lang.Integer"
什么是差异?
推荐答案
这是因为它无法分辨使用哪个atoi。 parseInt(String)和parseInt(String,int)有两种可能性。来自REPL:
This is because it can't tell which atoi to use. There are two possibilities parseInt(String) and parseInt(String, int). From the REPL:
scala> atoi <console>:7: error: ambiguous reference to overloaded definition, both method parseInt in object Integer of type (x$1: java.lang.String)Int and method parseInt in object Integer of type (x$1: java.lang.String,x$2: Int)Int match expected type ?
atoi
您需要具体说明使用哪一个,这将有效:
You need to say specifically which one to use, this will work:
println((args map ( atoi(_))).foldLeft(0)(_ + _));
这篇关于重命名导入的静态函数有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!