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

时间:2018-08-06 18:01:30

标签: generics casting kotlin polymorphism

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

2 个答案:

答案 0 :(得分:3)

如果有什么帮助,显然如果您用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);
            }
        });
    }
}

然后由于以下消息而无法使用:

  

原因:不存在类型变量的实例,因此Foo符合Bar   推断变量T具有不兼容的范围:   下限:Foo   上限: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?
            }
        }
    }
}

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

  

类型推断失败:没有足够的信息来推断参数T。请显式指定它。

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

class Blah: Foo(), Bar {
}

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

只有当我们知道特定的子类型是Foo和Bar的子类时,显然不能保证。


所以我似乎无法找到一种方法来使Kotlin将类强制转换为自己的类型,因此“相信我”可以将其安全地强制转换为T本身就是两者的子类Foo和Bar。

但是让Kotlin相信通过Java可以起作用:

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界面上使用@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

  
      
  • 使用-Xjvm-default = enable,仅为每个@JvmDefault方法生成接口中的默认方法。在这种模式下,使用@JvmDefault注释现有方法可能会破坏二进制兼容性,因为它将有效地从DefaultImpls类中删除该方法。
  •   具有-Xjvm-default = compatibility的
  • ,除了默认接口方法外,还在DefaultImpls类中生成了一个兼容性访问器,该访问器通过综合访问器调用默认接口方法。在这种模式下,使用@JvmDefault注释现有方法是二进制兼容的,但是会导致字节码中有更多方法。
  •   

此外,@JvmDefault要求target 1.8,但是Android逐渐拒绝使用应该现在处理默认接口。

答案 1 :(得分:1)

该错误消息有些模糊。让我们来帮助一下Kotlin编译器:

fun main(vararg args: String) {
     val list: List<Foo> = arrayListOf()
     list.forEach {
          val bar = it as Bar
          val c = Combiner().combine(bar) // inferred type Bar is not a subtype of Foo
     }
}

聪明的演员们正在做的就是-他们在铸造。将Foo投射到Bar之后,就无法保证它已经是Foo了。

相关问题