1

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

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.