Reputation: 2203
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?
Upvotes: 1
Views: 272
Reputation: 167881
The problem is that in Scala, Boolean <: AnyVal <: Any
, while java.lang.Boolean <: AnyRef <: Any
. Since <? super java.lang.Boolean>
means java.lang.Boolean
or any superclass of it, you must fall into the AnyRef
side of things. Unboxing is not enough; Boolean
still places you on the AnyVal
side of the type hierarchy even if you would box it into a java.lang.Boolean
.
Upvotes: 7