copy() для data class будет иметь ту же видимость, что и конструктор. В бета версии пока что покажут warning при использованииКак будет выглядеть:
data class PositiveInteger private constructor(val number: Int) {
companion object {
fun create(number: Int): PositiveInteger? = if (number > 0) PositiveInteger(number) else null
}
}
fun main() {
val positiveNumber = PositiveInteger.create(42) ?: return
// Будет вызывать Warning в 2.0.20-Beta1
val negativeNumber = positiveNumber.copy(number = -1)
// warning: non-public primary constructor is exposed via the generated 'copy()' method of the 'data' class.
}
Используете ли вы приватный конструктор у data классов?