gpt4 book ai didi

generics - 将参数传递给具有多个上限的泛型函数

转载 作者:IT老高 更新时间:2023-10-28 13:45:30 24 4
gpt4 key购买 nike

无法将参数传递给 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
}
}
}
}

这就是它与 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);
}
}
}
}

为 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);
}
});
}
}

然后由于以下消息它将无法工作:

reason: no instance(s) of type variable(s) exist so that Foo conforms to Bar inference variable T has incompatible bounds: lower bounds: Foo upper bounds: Foo, Bar

现在如果你将 cast 添加到 Bar:

        list.forEach(foo -> {
if(foo instanceof Bar) {
new Combiner().combine((Bar)foo);
}
});

问题很明显:(Bar)foo 现在是 Bar,而不是 Foo


因此,您需要知道一个确切的类型,它是 FooBar 的子类才能转换为它。

所以如果是这样的话,那么可以工作——事实上,在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));
}

事实上,下面的测试成功:

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");
}
});
}

这意味着理论上应该在 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?
}
}
}
}

除非它不起作用,因为它说:

Type inference failed: Not enough information to infer parameter T. Please specify it explicitly.

所以在 Kotlin 中,我所能做的就是:

class Blah: Foo(), Bar {
}

Combiner().combine(it.castToBar<Blah>())

这显然不能保证,只有当我们知道它的特定子类型是 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);
}
}

class Program {
fun main() {
val list: List<Foo> = arrayListOf()
list.forEach {
if (it is Bar) {
val pair = ForceCombiner.combine(it) // <-- should work

除了 ForceCombiner 现在只有当我们在 Kotlin 接口(interface)上使用 @JvmDefault 时才有效

interface Bar {
@JvmDefault
val b: String get() = "a"
}

现在说:

// Inheritance from an interface with `@JvmDefault` members is only allowed with -Xjvm-default option
class Blah: Foo(), Bar {
}

所以我实际上并没有尝试过 -Xjvm-default 选项,但是 it could work ?见 here how you can do that .

  • with -Xjvm-default=enable, only default method in interface is generated for each @JvmDefault method. In this mode, annotating an existing method with @JvmDefault can break binary compatibility, because it will effectively remove the method from the DefaultImpls class.
  • with -Xjvm-default=compatibility, in addition to the default interface method, a compatibility accessor is generated in the DefaultImpls class, that calls the default interface method via a synthetic accessor. In this mode, annotating an existing method with @JvmDefault is binary compatible, but results in more methods in bytecode.

另外,@JvmDefault 需要 target 1.8,但 Android 的脱糖应该现在处理默认接口(interface)。

关于generics - 将参数传递给具有多个上限的泛型函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51713278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com