[英]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.