Nir
08/19/2020, 4:48 PMdata class foo(val i: Int, val s: String)
val (s, i) = foo(5, "hello") // Reverse order you might expect, s is an int, i is a string
One could imagine doing it by name of the field instead. Imagine we use =, to piggy back off of the analogy between positional and named arguments
val (=s, =i) = foo(5, "hello") // okay, now it matches by name instead, so we create a variable s that's a string, i that's an int
It would require that there was a property with the name of the variable. if not, compiler error. behind the scenes it just gets translated to:
val unusable = foo(5, "hello")
val s = unusable.s
val i = unusable.i
Advantages would be that it would allow destructuring "safely", without worrying about getting the order right. It's a relatively minor/simple thing to implement, easy to understand at first glance, and has a similar benefit for correctness as named arguments over positional arguments.ephemient
08/19/2020, 5:20 PMephemient
08/19/2020, 5:21 PMwith(foo(5, "hello")) {
// i: Int
// s: String
}
Nir
08/19/2020, 6:00 PMNir
08/19/2020, 6:00 PMNir
08/19/2020, 6:01 PMNir
08/19/2020, 6:01 PMelizarov
08/20/2020, 12:25 PM=
is an interesting idea that I, personally, have not seen before. Thanks for throwing it in.elizarov
08/20/2020, 12:26 PMwith
and other scope functions), Kotlin (unlike some other languages) is not in a dire need to have this feature.elizarov
08/20/2020, 2:05 PMval (=
there’s no way IDE can know which class you are trying to destructure and what names should be suggested in auto-completion. This conflicts the whole Kotlin philosophy of being a tooling-friendly language.Nir
08/20/2020, 2:36 PMNir
08/20/2020, 2:36 PMwith
is, IMHO, a problem that I notice frequently in Kotlin.Nir
08/20/2020, 2:37 PMNir
08/20/2020, 2:38 PMwith
works great when you have a single object and you want to work with some variables from it with shorter names. If you have two objects and you want to pull some members from both, it does not work very wellNir
08/20/2020, 2:47 PMwith
to take multiple arguments, but that has the downside that you silently override parameter names as you work inwardsNir
08/20/2020, 2:51 PMelizarov
08/20/2020, 2:51 PMelizarov
08/20/2020, 2:53 PMwith
with multiple things to work, but it does not solve the problem of cherry-picking components.Nir
08/20/2020, 2:53 PMNir
08/20/2020, 2:54 PMNir
08/20/2020, 3:03 PMwith
work for multiple things would be huge, btw, assuming it's something more generalNir
08/20/2020, 3:04 PMNir
08/20/2020, 3:04 PMuse
, locking multiple mutexes in a designated order, etcelizarov
08/20/2020, 3:04 PMNir
08/20/2020, 3:04 PMstantronic
08/20/2020, 4:33 PMNico
08/21/2020, 6:22 PMNir
08/21/2020, 8:04 PMNir
08/21/2020, 8:04 PMfoo(s, i) = ...
Nir
08/21/2020, 8:04 PMNico
08/21/2020, 9:14 PMNir
08/21/2020, 9:18 PMNir
08/21/2020, 9:18 PM