Hi, everyone! I'm having trouble with the followin...
# codereview
m
Hi, everyone! I'm having trouble with the following code:
Copy code
data class Data<out T>(val value: T, private val secret: Int) {
    // Error: Type parameter T is declared as 'out' but occurs in 'in' position
    fun copy(value: T): Data<T> {
        GlobalLog.writeWarning("Secret just being copied!")
        return Data(value, secret)
    }
}
Compiler is complaining here about possible covariance violation. Obviously, `copy`'s implementation is safe. One workaround is to move it to extension function. But it requires exposing
secret
(or does it?). Another is to move copying to companion object. But you can't call
copy
with dot-syntax anymore (or can you?). Is there a way of rewriting it, such that
secret
is kept private and
copy
can be accessed with dot-syntax?