The javafx api is defined like this:
void addListener(ChangeListener<? super java.lang.Boolean> listener)
The following code..
new TextArea().focusedProperty.addListener(new ChangeListener[Boolean]() {
def changed(observable: ObservableValue[_ <: Boolean], oldValue: Boolean, newValue: Boolean) {
}
})
..gives this error:
overloaded method value addListener with alternatives:
(javafx.beans.value.ChangeListener[_ >: java.lang.Boolean])Unit
(javafx.beans.InvalidationListener)Unit cannot be applied to
(java.lang.Object with javafx.beans.value.ChangeListener[Boolean])
If I use java.lang.Boolean instead of Boolean, it works, but not with scala’s Boolean. Why is that? Is it possible to use this api without having to type the fully qualified name?
The problem is that in Scala,
Boolean <: AnyVal <: Any, whilejava.lang.Boolean <: AnyRef <: Any. Since<? super java.lang.Boolean>meansjava.lang.Booleanor any superclass of it, you must fall into theAnyRefside of things. Unboxing is not enough;Booleanstill places you on theAnyValside of the type hierarchy even if you would box it into ajava.lang.Boolean.