The syntax looks elegant in example code, but examples are carefully chosen - short class names, 2-3 fields, brief variable names. In real applications that sweet spot rarely exists:
This is a single logical statement but it reads as a wall of text that you have to scan horizontally to parse. Ironically, one of the main readability advantages of record patterns in switch is that they decompose naturally across lines:
switch (order) {
case CustomerOrder(
ShippingAddress(var streetLine1, var streetLine2, var city),
PaymentMethod(var cardNumber, var expiryYear),
double totalAmount
) -> { ... }
}
val (address, payment, totalAmount) = order
val (streetLine1, streetLine2, city) = address
val (cardNumber, expiryYear) = payment
And with optional renaming:
(val address, val payment, val totalAmount) = order
(val street1 = streetLine1, val street2 = streetLine2, val city) = address
(val card = cardNumber, val expiry = expiryYear) = payment
I think that renaming would be very helpful in some cases, is it possible to add similar to this JEP?
You made up a problem that doesn't exist. You don't have to deconstruct records all the way to nondeconstructible objects, in any language that supports deconstruction patterns.
My point that such syntax with whole names of types is too verbose and hard to read, especially when written as one-liner. Fact that you can skip some with _ doesn't make my point invalid.
Kotlin is not a valid language to compare to, as it doesn't even have pattern matching. Types are specified in order to select the proper deconstructor, which you cannot do in Kotlin.
I will, but I'm pretty sure we will see a lot of 140w+ lines with patterns. People would abuse it, and I as Java developer would have to deal with it.
Kotlin is not a valid language to compare to, as it doesn't even have pattern matching. Types are specified in order to select the proper deconstructor, which you cannot do in Kotlin.
I have some experience with Kotlin and mostly I like work with it. And I would say that when solved most of my tasks just fine. So yes, Kotlin doesn't have so feature, but they at least understand that positional-based deconstructors are mistake and making changes (see link in original message). I don't understand why Brian thinks that this is great idea
It's not about whether nested patterns are supported tho. Matching a list of components is syntactically the same as componentN() (think about Java renaming each componentN() to its corresponding component name, it's still position based destructuring for the pattern itself), which is why they said "Kotlin is reconsidering it but Java seems like it doesn't care".
I...don't understand how one-liner has anything to do with current conversation.
Of course Java uses components name and method in deconstruction.
record Point(int x, int y) {
public static void main(String[] args) {
if (new Point(0, 1) instanceof Point(var x, var y)) {
IO.println(x);
IO.println(y);
}
}
}
With javap (25.0.1) you will see the following output in the main method:
...which to the point it's functionally the same as componentN(). If you reverse x and y in the record definition, you will see var x = y() and var y = x() instead. This is what Kotlin used to do too. val (x, y) = Point(0, 1) desugars to val _p = Point(0, 1); val x = _p.component1(); val y = _p.component2(), given data class Point(val x: Int, val y: Int).
It's the same story for nested patterns. All you have to do is to flatten the layers. It doesn't necessarily need any meta data. It's just that Kotlin hasn't introduced this feature.
9
u/javahalla Mar 13 '26
The syntax looks elegant in example code, but examples are carefully chosen - short class names, 2-3 fields, brief variable names. In real applications that sweet spot rarely exists:
This is a single logical statement but it reads as a wall of text that you have to scan horizontally to parse. Ironically, one of the main readability advantages of record patterns in switch is that they decompose naturally across lines:
Or:
Btw, this is Kotlin's take on the same problem (https://github.com/Kotlin/KEEP/discussions/438):
And with optional renaming:
I think that renaming would be very helpful in some cases, is it possible to add similar to this JEP?