Why var with private setter is invariant position?

Viewed 644

(I use kotlin 1.1.2-2)

I found there are 2 ways to define property that is variable but cannot be assigned through =.

  1. var with private setter
  2. val with private variable backing property

I also found they have different behaviour.

When T is declared out, var of type T with private setter cannot be defined while val with backing property is legitimate.

open class A<out T>(v: T) {
    // error because T occurs in invariant position
    var prop1: T = v
    private set

    private var _prop: T = v
    val prop2: T get() = _prop
}

Why prop1 is invariant position and prop2 is not? Where does the difference come from?

1 Answers
Related