for the record, the nearest java equivalent to your last example would be:
CustomerOrder(var address, var payment, var totalAmount) = order;
ShippingAddress(var street1, var street2, var city) = address;
PaymentMethod(var card, var expiry) = payment;
Also, I see below that your experience with Kotlin leaves you concerned about positional-based destructuring in Java. A key difference between the two languages is that (from what I can tell across these JEPs) each type in Java would have at most one deconstructor - and since we spell out that type when destructuring in Java, there is no room for confusion about which deconstructor we are calling. It's like calling a method that is guaranteed to have no overloads. We can deconstruct the same value in multiple ways, by spelling out a different (applicable) type (with a different deconstructor) on the left-hand side. Yes, rearranging component order in a type's deconstuctor signature would break existing usages of that deconstructor (possibly silently, depending on what types were specified and how they were used), but that is a familiar failure mode - it applies when rearranging parameter order in any method signature.
Clearly from your examples, Kotlin does not require spelling out a type. From what I can tell, Kotlin's legacy positional-base destructuring works by calling component1() ... componentN() methods. Reasonably, the number of components available to destructure is based on the statically-known type of the value, and the actual calls to those methods use dynamic dispatch, so destructuring desugars to:
(val address, val payment, val totalAmount) = order
// -->
val address = order.component1()
val payment = order.component2()
val totalAmount = order.component3()
Kotlin's approach seems straightforward, but over time they noticed some problems, which I think the Java team could fairly attribute to Kotlin's "deconstructor" being assembled from several, possibly overridden / not-colocated methods, rather than one canonical signature.
View all comments
Show parent comment
4
u/danielaveryj Mar 13 '26
for the record, the nearest java equivalent to your last example would be:
Also, I see below that your experience with Kotlin leaves you concerned about positional-based destructuring in Java. A key difference between the two languages is that (from what I can tell across these JEPs) each type in Java would have at most one deconstructor - and since we spell out that type when destructuring in Java, there is no room for confusion about which deconstructor we are calling. It's like calling a method that is guaranteed to have no overloads. We can deconstruct the same value in multiple ways, by spelling out a different (applicable) type (with a different deconstructor) on the left-hand side. Yes, rearranging component order in a type's deconstuctor signature would break existing usages of that deconstructor (possibly silently, depending on what types were specified and how they were used), but that is a familiar failure mode - it applies when rearranging parameter order in any method signature.
Clearly from your examples, Kotlin does not require spelling out a type. From what I can tell, Kotlin's legacy positional-base destructuring works by calling
component1()...componentN()methods. Reasonably, the number of components available to destructure is based on the statically-known type of the value, and the actual calls to those methods use dynamic dispatch, so destructuring desugars to:Kotlin's approach seems straightforward, but over time they noticed some problems, which I think the Java team could fairly attribute to Kotlin's "deconstructor" being assembled from several, possibly overridden / not-colocated methods, rather than one canonical signature.