Any way to force use of named parameters on `copy`...
# announcements
m
Any way to force use of named parameters on
copy
for certain data classes? I have a class where first two props are `Int`s and wanted to change the second, but forgot to use name of property. Found after like 5 minutes.
s
Copy code
class Barrier internal constructor()

val barrier_ = Barrier()

data class Ugly(val i: Int, val j: Int) {
    fun safeCopy(barrier: Barrier = barrier_, i: Int = this.i, j: Int = this.j)
            = this.copy(i = i, j = j)
}

fun main(args: Array<String>) {
    Ugly(1, 2).safeCopy(1, 2) // compile time error
    Ugly(1, 2).safeCopy(i = 1, j = 2)
}
m
Interesting, but indeed ugly.
r
You could use the
Nothing
hack (
vararg barrier: Nothing
as the first parameter)
👍 1