object-destructuring-by-name_kt.kt
# language-proposals
j
object-destructuring-by-name_kt.kt
g
There is a feature request about this: https://youtrack.jetbrains.com/issue/KT-19627
And your proposal will not work, because it backward incompatible with existing syntax, issue above has alternative for this
j
@gildor what do you mean it would not work? It is an existing syntax that works right now. https://pl.kotl.in/kxzTHii2E
g
Yes, it works now, but it positioned destructuring, not named destructuring, Isn’t you propose to have named destructuring like in JS
j
I would really like the proposal you linked to be implemented, but in this case since you are just copy/pasting the same list of elements it’s hard to do a mistake.
g
Also you don’t need intermidiate list just use underscore: https://pl.kotl.in/EIoz3HETz
No need to copy/paste
j
Yes but your code breaks if I reorder the properties in the data class
g
Yes, that what I’m saying, that there is a feature request about this (see link above). And we need special syntax for named destructuring
Existing syntax will be always positioned
j
Then we both agree
g
One more problem for me with this feature (tho I support it in general) is that I think it would be nice, but pretty minor improvement comparing to:
Copy code
val city = person.city
val country = person.country
Or especially:
Copy code
println("City=${person.city} and Country=${person.country}")
Of course there are much more complicated cases, I just see not so many of them on practice But, because we already have positioned destrucutring, I would like to see named as a better alternative
j
I could see it being quite useful in a when clause for example:
Copy code
when(val {city, country} = person) {
   city.isGreat() && country in EUROPE -> ...
}
g
Copy code
if (person.city.isGreat() && person.country in EUROPE) {}
Also proably even more readable
j
If your when statement is large, it improves signifianctly the signal to noise ratio
g
Yes, that my point
if it’s large it will be not so great with destructuring too
Than I would just switch to extension functions, or local variables etc
Also if you format it properly it become even more readable even without destructuring
I don’t want to say that it’s bad, I just want to be realistic about this feature in general. Yeah, it’s kinda nice, but probably most of use cases of it wouldn’t pass our code review because it’s not very readable in many cases, even if it a bit shorter So it’s in the list of proposals for me that I would like to see, but probably they just too complex to implement and I would prefer to improve other features of Kotlin instead
j
I put two when statements together, don’t you first the first more readable? https://pl.kotl.in/zokVPr6bH
And yes it’s not a must-have, but still nice and I sort-of like my hack
g
Honestly I don’t see big difference in this case
also hack with list looks really bad for me, I mean I wouldn’t use it in a real code
Also there are ways to improve readability in your sample: person.city.hasTech().not() -> !person.city.hasTech() or even special extension:
Copy code
val techCities = listOf("Paris", "Berlin", "London", "New York")
person.city !in techCities
Which for me reads even better without destructuring Also
Copy code
city =="London" || city == "Paris" -> city in listOf("London", "Paris")
Or even:
Copy code
val expensiveCities = listOf("London", "Paris")

person.city in expensiveCities
j
@gildor One reason I like my pattern is that it is much more general. Destructuring only works with all properties of a data class (or tuple). Here you can have let say a
Service
that is not a data class and do
val (a,b,c) = with(service) { listOf(a, someFunctionReturningB(), this.a.c) }
g
I think it’s a lot more messy than:
Copy code
val a = service.a
val b = someFunctionReturningB() // or a.someFunctionReturningB()? One more reason to avoid scope in this case
val c = service.a.c
j
That’s also possible, I think that’s a matter of habit or taste? I like to minimize the number of statements as long as it’s readable. One thing though is that it’s better to use
Triple
instead of
listOf()
for when we have different types. I updated my example