N.Rubinstein
N.Rubinstein

Reputation: 13

Using generic type of interface in method

I'm getting confused with generics in Kotlin. How can I use T type of Class in function parameters (in addNewItem() and deleteItem())? I'm getting error "Type parameter T is declared as 'out' but occurs in 'in' position kotlin"

interface IStorageManager<out T: IFileItem> {

fun getAllItems(): List<T>

fun addNewItem(itemToAdd: T)

fun deleteItem(itemToDelete: T)

}

screen

Upvotes: 1

Views: 233

Answers (1)

s1m0nw1
s1m0nw1

Reputation: 82087

If your interface only produces T, you can make this clear to the compiler by annotating it with out. In your example you also have methods that act as consumers of T, thus out does not work. Just remove the out keyword and all will work fine.

This is a good reference: https://kotlinlang.org/docs/reference/generics.html

Upvotes: 3

Related Questions