Type-inference fails when binding ReactFx Var.mapBidirectional to JavaFx property

穿精又带淫゛_ 提交于 2019-12-12 02:17:48

问题


I am trying to bind a TextField.textProperty() to a ObjectProperty<LocalDateTime> in a custom control. The following code ompiles and runs in Eclipse:

package main.java

import java.time.LocalDateTime;

import org.reactfx.value.Var;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleStringProperty;

public class Playbook {
    public void bindTimeString(final ObjectProperty<LocalDateTime> timepoint, final SimpleStringProperty textProperty) {
        Var.mapBidirectional(textProperty, s -> LocalDateTime.now(), t -> "").bindBidirectional(timepoint);
    }

}

However, when I build my application with maven, I get a compilation error:

javac -classpath reactfx-2.0-M5.jar Playbook.java
Playbook.java:12: error: no suitable method found for bindBidirectional(ObjectProperty<LocalDateTime>)
        Var.mapBidirectional(textProperty, s -> LocalDateTime.now(), t -> "").bindBidirectional(timepoint);
                                                                             ^
    method Property.bindBidirectional(Property<Object>) is not applicable
      (argument mismatch; ObjectProperty<LocalDateTime> cannot be converted to Property<Object>)
    method Var.bindBidirectional(Property<Object>) is not applicable
      (argument mismatch; ObjectProperty<LocalDateTime> cannot be converted to Property<Object>)
1 error

A workaround is to declare a temporary Var<LocalDateTime holding the mapBidirectional result and then binding it.

public void bindTimeString(final ObjectProperty<LocalDateTime> timepoint, final SimpleStringProperty textProperty) {
    final Var<LocalDateTime> v = Var.mapBidirectional(textProperty, s -> LocalDateTime.now(), t -> "");
    v.bindBidirectional(timepoint);
}

compiles with Eclipse and from the command line with maven as expected.

It feels like a compiler bug in the type inference implementation, but I am not an expert for the Java language spec. I would expect that types can be inferred from the lambda return values. In any case, either Eclipse's java compiler or the JDK compiler is wrong.

来源:https://stackoverflow.com/questions/42245651/type-inference-fails-when-binding-reactfx-var-mapbidirectional-to-javafx-propert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!