【问题标题】:scala how to call overloaded method with correct signaturescala如何调用具有正确签名的重载方法
【发布时间】:2019-04-25 06:46:40
【问题描述】:

在 scala REPL 中使用 scala 2.10。我有两个myf 的定义,它们使用不同的参数类型重载。但是当我调用myf(第7行)时,它调用def myf(data:List[Int])而不是def myf(data:List[String])。尽管参数本身的类型是dataString:List[String]

如何在myf(data:List[Int]) 中调用myf(data:List[String])

我尝试使用(implicit d: DummyImplicit) 处理类型擦除,如here 所示

def myf(data:List[String]) : Unit = {
    data.foreach(println)
}

def myf(data:List[Int])(implicit d: DummyImplicit) : Unit = {
    val dataString:List[String] = data.map(_ + 1000).map(_.toString)     // do something else before toString
    myf(dataString:List[String])      // want to call myf(data:List[String]), does not want to call myf(data:List[Int])
}

val d:List[Int] = List(1,2,3)
myf(d)

错误:

Name: Compile Error
Message: <console>:50: error: type mismatch;
 found   : List[String]
 required: List[Int]
           myf(dataString:List[String])      // want to call myf(data:List[String]), does not want to call myf(data:List[Int])

【问题讨论】:

  • 为什么? “鉴于 JVM 的限制,为什么要尝试模拟永远无法正常工作的东西?”为什么不给这两个函数两个不同的名字呢?基于参数类型的重载已经够变幻无常了,尤其是类型擦除。痛苦到底是为了什么?
  • 这不会在 REPL 或 IntelliJ 工作表中运行,因为每个编译单元都包含在自己的 object 中。因此myf() 的第二个定义掩盖了第一个。您的代码在包装在 object 中时运行良好,使整个代码成为具有 myf() 的 2 个定义的编译单元。
  • 我确实在 REPL 中运行
  • @jwvh 我尝试使用 main() 方法在 .scala 文件中编写相同的代码,它给出了相同的编译错误..
  • @SidaZhou;我能够以各种方式运行您的代码,而无需更改: 1-通过:paste 按原样粘贴到 REPL 中。 2-在 IDE 工作表中的 object 内。 3-在object.scala 文件中。在最后一种情况下,只有最后两行发布的代码进入 main() 方法。

标签: scala


【解决方案1】:

这是一个运行您的代码的 REPL 会话。

ShellPrompt> scala  #a fresh REPL session
Welcome to Scala 2.12.7 (OpenJDK 64-Bit Server VM, Java 11.0.2).
Type in expressions for evaluation. Or try :help.

scala> :paste
// Entering paste mode (ctrl-D to finish)

def myf(data:List[String]) : Unit = {
    data.foreach(println)
}

def myf(data:List[Int])(implicit d: DummyImplicit) : Unit = {
    val dataString:List[String] = data.map(_ + 1000).map(_.toString)     // do something else before toString
    myf(dataString:List[String])      // want to call myf(data:List[String]), does not want to call myf(data:List[Int])
}

val d:List[Int] = List(1,2,3)
myf(d)

// Exiting paste mode, now interpreting.

1001
1002
1003
myf: (data: List[String])Unit <and> (data: List[Int])(implicit d: DummyImplicit)Unit
myf: (data: List[String])Unit <and> (data: List[Int])(implicit d: DummyImplicit)Unit
d: List[Int] = List(1, 2, 3)

这是一个文件编译演示。

ShellPrompt> cat so.sc
object SO {
  def myf(data:List[String]) : Unit = {
    data.foreach(println)
  }

  def myf(data:List[Int])(implicit d: DummyImplicit) : Unit = {
    val dataString:List[String] = data.map(_ + 1000).map(_.toString)
    myf(dataString:List[String])
  }

  def main(args:Array[String]): Unit = {
    val d:List[Int] = List(1,2,3)
    myf(d)
  }
}
ShellPrompt> scalac so.sc -deprecation -explaintypes -feature -unchecked -Xlint -Ypartial-unification
ShellPrompt> scala SO
1001
1002
1003
ShellPrompt>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多