hmm, I still don't feel like using an `out` type a...
# announcements
m
hmm, I still don't feel like using an
out
type as an argument of a method is something semantically incorrect on its own. There should be something else. For example, the following code successfully compiles:
Copy code
class Value<out T>(val src: T) {
    private fun display(t: T) {
        println(t)
    }

    fun displaySelf() {
        display(src)
    }
}
here I understand why a private method may have
T
as an argument: compiler is perfectly sure
Value
is still covariant.
Value<Int>
could be used as
Value<Any?>
and
String
could be passed as
Any?
, but a String can never be passed to
Value<Int>.display()
. This is because only true
Value<Int>
instance may call its own
display()
. Compiler doesn't allow exposing a contravariant or invariant type out of covariant type (I can't declare
fun getReceiver(): Receiver<T>
within
Value<out T>
). Thus I cannot give away an instance of my anonymous
Receiver<T>
implementation out of
Value<out T>
instance. I also cannot cast it to
Receiver<Any?>
because
Receiver
is contravariant. With all this in mind, it seems to me its perfectly safe (from variance point of view) to treat methods of anonymous classes the same way private methods are. Maybe I'm missing something?