vendredi 10 novembre 2017

Implementing a kotlin interface in java

So, after this question where I basically exploits reflection for passing primitive references to modify the primitive itself, like:

_begin("Another Window", ::showAnotherWindow)

I was looking for something to make something similar possible also from java, where at the moment I am using plains primitive arrays:

private boolean[] showAnotherWindow = {false};
imgui.begin("Another Window", showAnotherWindow);

@hotkey suggested me the possibility to create a class implementing the KMutableProperty0 interface and that automatically gets and sets the corresponding variable

Example:

KMutableProperty0<Boolean> prop = 
  PropUtils.javaProp(this, t -> t.showAnotherWindow, (t, r) -> { t.showAnotherWindow = r; });

_begin("Another Window", prop);

So, I wanted to give it a try and implemented the following in java.

Getter:

@FunctionalInterface
public interface Getter<T> {

    T get();
}

Setter:

@FunctionalInterface
public interface Setter<T> {

    void set(T type);
}

And then the class itself (I just wrote the constructor, all the methods are those requested by the interface and automatically implemented by the IDE) :

public class JavaProp <T> implements KMutableProperty0<T> {

    private imgui.Getter<T> getter;
    private imgui.Setter<T> setter;

    public JavaProp(imgui.Getter<T> getter, imgui.Setter<T> setter) {
        this.getter = getter;
        this.setter = setter;
    }

    @Override
    public void set(T t) {
        setter.set(t);
    }

    @NotNull
    @Override
    public Setter<T> getSetter() {
        return null;
    }

    @Override
    public T get() {
        return getter.get();
    }

    @Nullable
    @Override
    public Object getDelegate() {
        return null;
    }

    @NotNull
    @Override
    public Getter<T> getGetter() {
        return null;
    }

    @Override
    public T invoke() {
        return null;
    }

    @Override
    public boolean isLateinit() {
        return false;
    }

    @Override
    public boolean isConst() {
        return false;
    }

    @NotNull
    @Override
    public String getName() {
        return null;
    }

    @NotNull
    @Override
    public List<KParameter> getParameters() {
        return null;
    }

    @NotNull
    @Override
    public KType getReturnType() {
        return null;
    }

    @NotNull
    @Override
    public List<KTypeParameter> getTypeParameters() {
        return null;
    }

    @Override
    public T call(Object... objects) {
        return null;
    }

    @Override
    public T callBy(Map<KParameter, ?> map) {
        return null;
    }

    @Nullable
    @Override
    public KVisibility getVisibility() {
        return null;
    }

    @Override
    public boolean isFinal() {
        return false;
    }

    @Override
    public boolean isOpen() {
        return false;
    }

    @Override
    public boolean isAbstract() {
        return false;
    }

    @NotNull
    @Override
    public List<Annotation> getAnnotations() {
        return null;
    }
}

But whenever I try to run that, I get the following:

Error:(45, 12) java: reference to Getter is ambiguous

both interface kotlin.reflect.KProperty0.Getter in kotlin.reflect.KProperty0 and interface kotlin.reflect.KProperty.Getter in kotlin.reflect.KProperty match

The problematic function is this one:

  @NotNull
  @Override
  public Getter<T> getGetter() {
      return null;
  }

And the relevant file is kotlin.reflect.KProperty.tk, you can find it here

Any idea how could I solve it?





Aucun commentaire:

Enregistrer un commentaire