*Proposal*: Allow named parameters in `get` and `s...
# language-proposals
r
Proposal: Allow named parameters in
get
and
set
operators Currently, you can have default parameters in
get
and
set
operators, but you cannot address them out of order. For example, given this:
Copy code
class Thing<T>(...) {
    operator fun set(coordinates: IntArray, depth: Int = this.depth, isDestructive: Boolean = true, value: T) { ... }
}
Which allows this:
Copy code
thing[coordinates] = value
But it would be nice to be able to do this:
Copy code
thing[coordinates, isDestructive = false] = value
However, there doesn't appear to be a way to do so. Instead, I'm stuck either adding a second
set
operator, or doing
Copy code
thing[coordinates, thing.depth, false] = value
(Which only works assuming
thing.depth
is accessible. If not, and there isn't an overloaded
set
operator, I don't believe it's possible.) Yes, I am aware this can be simply worked around by calling the set function directly and not using the operator, which is why this is a proposal and not a bug report.
2
Also, it currently appears to be impossible to use a signature like
Copy code
operator fun get(vararg coordinates: Int, depth: Int = this.depth)
k
Yes please! I would love this on Kotlin! Swift has this and I've used it to great effect: https://github.com/RougeWare/Swift-Safe-Collection-Access