Scala compile time error, missing parameter type

443 views Asked by At

I am trying to get a dialogue to get numeric input using this code

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)
    }))
}

but get the missing parameter type compilation error

[error]         editor.setTextFormatter(new TextFormatter( c => {
[error]                                                    ^

No idea what the parameter type should be and despite much googling cant find any useful hints.

IntelliJ does not think there is anything wrong but sbt gives the error on compile.

1

There are 1 answers

2
ELinda On

The constructor being used is of a UnaryOperator type, so the TextFormatter.Change => TextFormatter.Change type should be compatible.

With the Scala 2.12 REPL, this will work (using the above constructor signature with UnaryOperator):

import javafx.scene.control.TextFormatter
val tf = new TextFormatter((c: TextFormatter.Change) => c)

The missing type error may occur if the compiler cannot infer the matching types to a constructor, which may happen if you have an incorrect import, or possibly even an older version of Scala which doesn't match with the version being used by your IDE.

Since you say that IntelliJ did not find a problem, it seems like it's more likely the latter. Please check that your project settings in your IDE match with scalaVersion in build.sbt.

You may also want to reduce the inference that must be done by the compiler, by explicitly defining some of the terms. For example you can try explicit definition of a UnaryOperator:

import java.util.function._
val uo: UnaryOperator[TextFormatter.Change] = UnaryOperator.identity[TextFormatter.Change]()
val tf = new TextFormatter(uo)