如何在Kotlin中制作单例空的不可变“类型安全”集合?

时间:2018-06-11 17:54:49

标签: java types kotlin type-conversion type-inference

我在Java和现在的Kotlin中策划了一些不可变的集合。我经常创建一个空的不可变集合的单例实例,因为Java具有类型擦除,并且该实例是相同的,并且可以安全地共享所有集合。

在Java中,您可以创建一个非泛型的单例对象并将其包装在一个函数中,以便在从泛型类型安全代码调用时,使类型推断适用于任何类型。我从java.util.Collections class

中采用了这种技术
//                      v-No generic types specified-v
public static final List EMPTY_LIST = new Collections.EmptyList();

//                   v-------v-- generic type "passes through"
public static final <T> List<T> emptyList() {
    return EMPTY_LIST;
}

所以这是我自己的Java代码,看起来非常相似(并且有效):

public static final Xform EMPTY = new SourceProviderIterableDesc<>(Collections.emptyList());

@SuppressWarnings("unchecked")
public static <T> Xform<T> empty() {
    return (Xform<T>) EMPTY;
}

这里它被转换为Kotlin:

private val EMPTY: Xform<Nothing> = SourceProviderIterableDesc(emptyList())
@Suppress("UNCHECKED_CAST")
internal fun <T> empty(): Xform<T> =
        EMPTY as Xform<T>

我收到了这个错误:

java.lang.ExceptionInInitializerError
    at org.organicdesign.fp.xform.XformTest.construction(XformTest.kt:529)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: kotlin.TypeCastException: null cannot be cast to non-null type org.organicdesign.fp.xform.Xform<T>

如何在Kotlin中使用add-a-type-to-something-no-type技巧?还有这个名字吗?

注意:如果我在Kotlin中使用listOf()而不是emptyList(),我会收到同样的错误。

0 个答案:

没有答案