Avatar ·

What does the out keyword mean in Kotlin and how to use it with generics?

Hi! I started learning Kotlin after Java and encountered a construct I don't understand. In Java, I'm used to generics with wildcards like `? extends T` and `? super T`. But in Kotlin, I see the `in` and `out` keywords directly in the class declaration. For example, I came across this code: ```kotlin interface Source { fun get(): T } ``` What does `out` before `T` mean? I tried writing my own class without `out`: ```kotlin class Box(private val item: T) { fun get(): T = item fun set(newItem: T) { /* ... */ } } ``` And it works. But if I add `out`, the compiler complains about the `set` method. Why? And what is `out` even needed for? In what situations should it be used? How is this related to the PECS principle (Producer Extends, Consumer Super) from Java? Please explain.