Skip to content

Instantly share code, notes, and snippets.

@PaulWoitaschek
Created February 6, 2017 08:23
Show Gist options
  • Save PaulWoitaschek/4ac68aaa08d53f7e87d3379054a231f4 to your computer and use it in GitHub Desktop.
Save PaulWoitaschek/4ac68aaa08d53f7e87d3379054a231f4 to your computer and use it in GitHub Desktop.
Kotlin interop failue
//////////////// java base class
public final class GenericInterop {
public interface BaseChanger<From, To> {
Base<To> change(Base<From> from);
}
public static class Base<T> {
public final <R> Base<R> compose(BaseChanger<? super T, ? extends R> changer) {
//noinspection unchecked
BaseChanger<T, R> casted = (BaseChanger<T, R>) changer;
return casted.change(this);
}
}
}
///////////// java knows type diamonds:
public GenericInterop.Base<String> transform(GenericInterop.Base<Integer> intBase) {
return intBase.compose(from -> new GenericInterop.Base<>());
}
/////////// kotlin doesnt know type, need to specify <String>
fun transform(intBase: GenericInterop.Base<Int>): GenericInterop.Base<String> {
return intBase.compose { GenericInterop.Base<String>() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment