[英]Scala compile time error, missing parameter type
我正在尝试使用此代码进行对话以获取数字输入
val dialog: TextInputDialog = new TextInputDialog{
initOwner(Main.stage)
title = "Set Programme Counter"
headerText = "Numeric format not supported."
contentText = "New PC value:"
import java.text.DecimalFormat
val format = new DecimalFormat("#.0")
import java.text.ParsePosition
editor.setTextFormatter(new TextFormatter( c => {
def foo(c:TextFormatter.Change): TextFormatter.Change = {
if (c.getControlNewText.isEmpty) return c
val parsePosition = new ParsePosition(0)
val o = format.parse(c.getControlNewText, parsePosition)
if (o == null || parsePosition.getIndex < c.getControlNewText.length) null
else c
}
foo(c)
}))
}
但得到缺少的参数类型编译错误
[error] editor.setTextFormatter(new TextFormatter( c => {
[error] ^
不知道参数类型应该是什么,尽管进行了很多谷歌搜索,但找不到任何有用的提示。
IntelliJ 认为没有任何问题,但 sbt 给出了编译错误。
使用的构造函数是UnaryOperator 类型,因此TextFormatter.Change => TextFormatter.Change
类型应该是兼容的。
使用 Scala 2.12 REPL,这将起作用(使用上述带有UnaryOperator
的构造函数签名):
import javafx.scene.control.TextFormatter
val tf = new TextFormatter((c: TextFormatter.Change) => c)
如果编译器无法将匹配类型推断为构造函数,则可能会发生丢失类型错误,如果您的导入不正确,或者甚至可能是与 IDE 使用的版本不匹配的旧版本 Scala 可能会发生这种情况。
既然你说 IntelliJ 没有发现问题,看来后者更有可能。 请检查 IDE 中的项目设置是否与scalaVersion
中的 scalaVersion 匹配。
您可能还希望通过显式定义一些术语来减少编译器必须完成的推理。 例如,您可以尝试明确定义UnaryOperator
:
import java.util.function._
val uo: UnaryOperator[TextFormatter.Change] = UnaryOperator.identity[TextFormatter.Change]()
val tf = new TextFormatter(uo)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.