Reputation: 13
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)
}
Upvotes: 1
Views: 233
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