And*_*sik 6 generics polymorphism casting kotlin
无法传递参数来Combiner().combine()起作用.
Android Studio无法识别arg扩展Foo和实现Bar.我究竟做错了什么?
abstract class Foo {
val f: Int = 1
}
interface Bar {
val b: String get() = "a"
}
class Combiner {
fun <T> combine(arg: T): Pair<Int, String> where T : Foo, T : Bar {
return arg.f to arg.b
}
}
class Program {
fun main() {
val list: List<Foo> = arrayListOf()
list.forEach {
if (it is Bar) {
Combiner().combine(it) //inferred type Any is not a subtype of Foo
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这就是它如何与Java一起使用:
public static class Program {
public static void main() {
List<Foo> list = new ArrayList<>();
for (Foo item : list) {
if (item instanceof Bar) {
new Combiner().combine((Foo & Bar) item);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
为Kotlin创建了错误报告:https://youtrack.jetbrains.com/issue/KT-25942
如果这有任何帮助,显然如果你用 Java 写同样的东西:
abstract class Foo {
public int getF() {
return 1;
}
}
interface Bar {
default String getB() {
return "a";
}
}
static class Combiner {
public <T extends Foo & Bar> Pair<Integer, String> combine(T arg) {
return Pair.create(arg.getF(), arg.getB());
}
}
public static class Program {
public static void main() {
List<Foo> list = new ArrayList<>();
list.forEach(foo -> {
if(foo instanceof Bar) {
new Combiner().combine(foo);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
然后它将无法工作,因为出现以下消息:
原因:不存在类型变量的实例,因此 Foo 符合 Bar 推断变量 T 具有不兼容的边界:下限:Foo 上限:Foo、Bar
现在,如果您添加cast到Bar:
list.forEach(foo -> {
if(foo instanceof Bar) {
new Combiner().combine((Bar)foo);
}
});
Run Code Online (Sandbox Code Playgroud)
问题很明显:(Bar)foo现在是 a Bar,而不是 a Foo。
因此,您需要知道一个确切的类型,它是两者的子类Foo,Bar以便转换为它。
因此,如果是这种情况,那么以下内容可以工作 - 事实上,在 Java 中,它实际上可以编译:
public static <T extends Foo & Bar> T toBar(Foo foo) {
//noinspection unchecked
return (T)foo;
}
public static class Program {
public static void main() {
List<Foo> list = new ArrayList<>();
list.forEach(foo -> {
if(foo instanceof Bar) {
new Combiner().combine(toBar(foo));
}
Run Code Online (Sandbox Code Playgroud)
事实上,以下测试是成功的:
public static class Pair<S, T> {
public Pair(S first, T second) {
this.first = first;
this.second = second;
}
S first;
T second;
public static <S, T> Pair<S, T> create(S first, T second) {
return new Pair<>(first, second);
}
}
public static <T extends Foo & Bar> T toBar(Foo foo) {
//noinspection unchecked
return (T)foo;
}
public class Blah extends Foo implements Bar {
}
@Test
public void castSucceeds() {
Blah blah = new Blah();
List<Foo> list = new ArrayList<>();
list.add(blah);
list.forEach(foo -> {
if(foo instanceof Bar) {
Pair<Integer, String> pair = new Combiner().combine(toBar(foo));
assertThat(pair.first).isEqualTo(1);
assertThat(pair.second).isEqualTo("a");
}
});
}
Run Code Online (Sandbox Code Playgroud)
这意味着理论上以下内容应该在 Kotlin 中工作:
class Program {
fun main() {
val list: List<Foo> = arrayListOf()
list.forEach {
if (it is Bar) {
@Suppress("UNCHECKED_CAST")
fun <T> Foo.castToBar(): T where T: Foo, T: Bar = this as T
Combiner().combine(it.castToBar()) // <-- magic?
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为它说:
类型推断失败:没有足够的信息来推断参数 T。请明确指定。
所以在 Kotlin 中,我能做的就是:
class Blah: Foo(), Bar {
}
Combiner().combine(it.castToBar<Blah>())
Run Code Online (Sandbox Code Playgroud)
这显然是不能保证的,除非我们知道它的具体子类型是 Foo 和 Bar 的子类。
所以我似乎找不到一种方法让 Kotlin 将一个类转换为它自己的类型,因此“相信我”它可以安全地转换为它T本身以及 Foo 和 Bar 的子类。
但通过 Java 让 Kotlin 相信它是可行的:
import kotlin.Pair;
public class ForceCombiner {
private ForceCombiner() {
}
private static <T extends Foo & Bar> Pair<Integer, String> actuallyCombine(Bar bar) {
T t = (T)bar;
return new Combiner().combine(t);
}
public static Pair<Integer, String> combine(Bar bar) {
return actuallyCombine(bar);
}
}
Run Code Online (Sandbox Code Playgroud)
和
class Program {
fun main() {
val list: List<Foo> = arrayListOf()
list.forEach {
if (it is Bar) {
val pair = ForceCombiner.combine(it) // <-- should work
Run Code Online (Sandbox Code Playgroud)
但现在只有在 Kotlin 界面上ForceCombiner使用时才有效@JvmDefault
interface Bar {
@JvmDefault
val b: String get() = "a"
}
Run Code Online (Sandbox Code Playgroud)
现在说:
// Inheritance from an interface with `@JvmDefault` members is only allowed with -Xjvm-default option
class Blah: Foo(), Bar {
}
Run Code Online (Sandbox Code Playgroud)
所以我实际上没有尝试过-Xjvm-default选项,但它可以工作吗?请参阅此处如何做到这一点。
- 使用 -Xjvm-default=enable 时,仅为每个 @JvmDefault 方法生成接口中的默认方法。在此模式下,使用 @JvmDefault 注解现有方法可能会破坏二进制兼容性,因为它将有效地从 DefaultImpls 类中删除该方法。
- 使用 -Xjvm-default=compatibility,除了默认接口方法之外,还会在 DefaultImpls 类中生成兼容性访问器,该访问器通过合成访问器调用默认接口方法。在此模式下,使用 @JvmDefault 注解现有方法是二进制兼容的,但会在字节码中产生更多方法。
另外,@JvmDefault需要target 1.8,但 Android 的脱糖现在应该可以处理默认接口。
| 归档时间: |
|
| 查看次数: |
250 次 |
| 最近记录: |