Nir
12/21/2020, 5:23 PMjlw
12/21/2020, 5:36 PMdiesieben07
12/21/2020, 5:38 PMcopy will still call the constructor and the constructor includes any init { } blocks. For example you can do this:
data class Foo(val nonNegative: Int) {
init {
require(nonNegative >= 0) { "nonNegative must be >= 0" }
}
}
You will never have a Foo(nonNegative=-3) with this class, even when using copy. copy(nonNegative=-3) will still run the init block and throw any exceptions.Nir
12/21/2020, 5:54 PMdiesieben07
12/21/2020, 5:55 PMcopy signature.Nir
12/21/2020, 6:00 PMNir
12/21/2020, 6:00 PMNir
12/21/2020, 6:01 PMdata class Point<D : Dimension> private constructor(val data: List<Int>) {
constructor(d: D, init: (Int) -> Int) : this(List(d.dimension, init))
}Nir
12/21/2020, 6:01 PMNir
12/21/2020, 6:02 PMcopy function doesn't exist, there's just no way to create a Point<D> , that doesn't obey the invariant that D's dimension == data.sizeNir
12/21/2020, 6:03 PM