Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use generics in companion object

I'd like to use generics in companion object in this way:

class Foo<T> {
    /* ... */
    companion object {
        fun foo(args: List<T>) {
            /* ... */
        }
    }
}

Unfortunately the code above raise Unresolved reference: T error.

like image 360
Mpac Avatar asked Nov 02 '17 21:11

Mpac


1 Answers

You either need to declare the generic like so

fun <T> foo(args: List<T>) { ... }

or, if you don't care about the type, you can use a star projection

fun foo(args: List<*>) { ... }
like image 109
Ruckus T-Boom Avatar answered Sep 28 '22 16:09

Ruckus T-Boom