I'm trying to understand Kotlin take of generics especially with the in
and out
keywords. I've written this simple piece to hopefully illustrate my point of confusion.
This is the method that looks and works OK;
Here, IDE gives a hint saying that the type of the item the for loop is iterating over is a Shape
. This is good.
Another version of the method above that doesn't seem quite right at first glance (since it's a "consumer"). But all I want to do is to read a list of shapes and that's all what's in this from
.
That doesn't work which is maybe right according to the rules but I am not able to connect the dots here. It seems like a safe operation. I can also see Kotlin downs the type to "Any?" which explains why this gives a compiler error already. Can someone explain the potential type unsafety here please?