-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Add support for tuple destructuring #485
Comments
Hi @CC007. All great feature requests. Regarding, the LocalDateTime.now() question, I'm not seeing that. auto returnTuple() {
return "foo", 42, 13.37, LocalDateTime.now();
} Works fine for me, both in IJ editor & compilation. |
Right. That is intentional. Tuples infer labels from the expressions if possible. In this case the method name. |
It is a bit unintuitive though that item4 couldn't be used as well as an alias. |
Yep. But I think label inference tends to be more useful than not because:
|
Those are indeed all good reasons why label inference is intuitive to have. What I found unintuitive though was not that there is inference, but the lack of the item4. I'm a proponent of having both. |
Sure, that makes sense. Always have the Would be interesting to make array operator work where a compile-time constant index expression would determine the type: String item1 = tuple[0];
int item2 = tuple[1]; lol |
Agreed. Actually wanted to add that as a feature request too, but considering the type differences, I could see that being an issue. |
Any progress on implementing this feature? |
Is your feature request related to a problem? Please describe.
Consider the following method:
When you want to use specific components of this tuple, it becomes quite verbose:
Describe the solution you'd like
It would be nice if manifold supported tuple destructuring, like so:
This would also be a nice feature for interacting with records, when you want to more formally define and document the data structure. Consider this:
In the case that the tuple/record has more items than the destructuring requests, the extra items should be ignored. Maybe
_
could be used for explicitly ignoring/skipping certain elements during destructuring, like so:Describe alternatives you've considered
The syntax could also be like this for when you don't want to use type inference:
Destructuring classes in general would technically also be possible, but would be way more complex and therefore out of scope, due to having to decide which fields/methods to make available as items, inheritance and an unclear ordering of the items. Records are straight-forward: they have no inheritance, all items are specified as record parameters, in a specific order and they have a getter by default.
Additional context
See documentation for tuple deconstructing/destructuring in other languages:
JEP for java 21 for destructuring records when using
instanceof
: https://openjdk.org/jeps/440preview JEP for java 21 for unnamed patterns and variables: https://openjdk.org/jeps/443
PS: why are the extra brackets needed around
LocalDateTime.now()
? It doesn't compile without them.The text was updated successfully, but these errors were encountered: