https://kotlinlang.org logo
Title
s

Syed Faraz

07/16/2019, 9:10 AM
sealed class Operation <out T> {

   data class InsertOperation<out T>(var insertedData: ArrayList<T>)
}
Hi, this gives
type parameter t is declared as 'out' but occurs in 'invariant' position in type t
, can anyone tell me what is correct way of handling this type of variance checking?
d

diesieben07

07/16/2019, 9:12 AM
An
out
parameter means you are only getting values of that type out of the class. But
ArrayList
permits writing. Use a
List<T>
, which is not modifiable
s

Syed Faraz

07/16/2019, 9:15 AM
my first thought was that so i have checked using list as well, but it throws the same error unfortunately. is there anything i'm missing here?
d

diesieben07

07/16/2019, 9:16 AM
You also have
insertedData
declared as a
var
, which means you can, again, put data in.
m

max

07/16/2019, 9:41 AM
is the second
<out T>
really necessary?
s

Syed Faraz

07/16/2019, 9:46 AM
@Massimo yes, we need it, otherwise we'll not be able use it,
data class InsertOperation<out T>(val insertedData: List<T>) :Operation<T>
it'll be something like this
@diesieben07 thanks it's working now.
m

max

07/16/2019, 10:51 AM
@Syed Faraz thanks you're right, in your snippet is the first one to be unused