如何在伴侣对象中使用泛型

Mpa*_*pac 10 generics kotlin

我想以这种方式在伴随对象中使用泛型:

class Foo<T> {
    /* ... */
    companion object {
        fun foo(args: List<T>) {
            /* ... */
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,上面的代码引发了Unresolved reference: T

Ruc*_*oom 11

您需要像这样声明泛型

fun <T> foo(args: List<T>) { ... }
Run Code Online (Sandbox Code Playgroud)

或者,如果您不关心类型,可以使用星形投影

fun foo(args: List<*>) { ... }
Run Code Online (Sandbox Code Playgroud)