More Flexible Component Functions

Hi,

I would like to suggest an addition to the component function feature of the Kotlin language. Currently, we can define component functions like

operator fun component1() = this[0]

for example, and this is a really great feature for destructuring declarations.
However, I think it would also make sense to allow a function like:

operator fun componentN(n: Int) = this[n-1]

This would make the system much more flexible for containers like lists and arrays, and would reduce the need for a lot of repeated code. If a class has both a component1 and a componentN function then I suggest the component1 function would take priority, but the componentN would be used whenever there is not a corresponding operator method for that specific component.

I would be grateful to hear your thoughts.

2 Likes

https://youtrack.jetbrains.com/issue/KT-17147

2 Likes

Ah sorry. I couldn’t find that.

1 Like