6

(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?

2
  • The longer I look, the more it looks like a bug to me :/
    – voddan
    Jul 1, 2017 at 9:39
  • Try to add "private get" for the prop1 variable and check again. The "private set" maybe could add an incosistent object which will be returned outside of the class.
    – LiTTle
    Jul 1, 2017 at 10:34

1 Answer 1

2

In your case you declare the private var can works is that you can't change it out of the class A since it is private, and you can't declare a function with a out variance parameter for the modification purpose.

The different between private var and private set is a private variable don't has getter/setter just generated a field in java. but private set properties have getter/setter and the setter is private.

The out variance is only for the read-mode, which means you can't add anything in it. and its actually type is a subtype of T, or ? extends T in java.

For the write-mode of the out variance is equivalent to Nothing, so you can't declare the setter/mutable variable at all. but you can reference it with an immutable property, for example:

open class A<out T>(v: T) {
 //v--- immutable
  val prop1: T = v

}

If you can do it, the kotin generic is a bad thing. why? by definition,out T is a subtype of T, but you attempt to assign a supertype instance T to a subtype of ? extends T, for example:

val subInt:A<Int> = A(1);
//             v--- Int
subInt.prop1 = 1;  // you try to assign an Int to its subtype
//     ^--- prop1 is a subtype of Int

Maybe the following example will makes you more clearly why can't adding anything into a out variance parameter.

val int: A<Int> = A(1) // ok

val number: A<Number> = int; //ok

number._prop = 1.0; 
//     ^
//if you can define setter/mutable variable, you try to assign a Double into a Int 
10
  • Not having a getter/setter seems a minor implementation detail, not enough to explain the differens in type system semantics
    – voddan
    Jul 1, 2017 at 9:37
  • @voddan yes, sir. Indeed, this behavior private var prop:T = v should be prohibited in kotlin.
    – holi-java
    Jul 1, 2017 at 12:34
  • 1
    There is no need to prohibit it. Kotlin is aware that allowing private var prop: T where T is a covariant (out) type parameter may lead to type system violation, so these properties are treated as "private to this", e.g. a class is allowed to change them only on its own instance. Jul 4, 2017 at 13:45
  • @RomanElizarov hi, as you said it can changed in its own instance, so the behavior should be prohibited. let's say you have a function that you can change it internally, for example: _prop = 1 as T, the code is unreliable if the T is not a Int.
    – holi-java
    Jul 4, 2017 at 14:00
  • Can you, please, provide a self-contained example that demonstrates type system violation because of mutable properties with covariant type? Jul 11, 2017 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.