Type-inference fails when binding ReactFx Var.mapB

2019-09-15 05:20发布

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.

0条回答
登录 后发表回答