ursus
08/06/2024, 12:02 AM(a, b)
coming to Kotlin, same like Swift has?ephemient
08/06/2024, 12:10 AMPair
and Triple
and prevailing style is to create your own named data class
for each needephemient
08/06/2024, 12:11 AMursus
08/06/2024, 12:11 AMephemient
08/06/2024, 12:12 AMursus
08/06/2024, 12:14 AMlistOf(..)
to [..]
smells lot like Pair(…)
to (…)
CLOVIS
08/06/2024, 7:57 AM(…)
will ever be added, it looks too much like existing syntax.
However, I wouldn't be surprised that pairs benefit from the collection literals syntax:
val a: Pair<Int, String> = [5, "foo"]
Stephan Schröder
08/06/2024, 10:05 AMto
, so it's: a to b
exampleursus
08/06/2024, 10:19 PMa to b to c to d
is just letter soup 😕ephemient
08/06/2024, 10:38 PMursus
08/06/2024, 11:21 PMTriple(a,b,c)
? I guess the normal (..)
syntax is even more necessary...Skaldebane
08/07/2024, 12:11 AM(...)
conflicts with existing syntax.
Assuming you're talking about destructuring syntax, destructuring itself is kinda like a stripped down and limited version of extracting a tuple's fields.
The Kotlin team has made it clear that if they implement this, it will be named tuples, and they're basically just objects without a name, which can be very useful for many use-cases.
Rust has un-named tuples, and while they're not used a lot as return types, they make some APIs very clean (e.g. partition
splits a list in two parts, so it returns a (Vec<T>, Vec<T>)
tuple, instead of a list of lists).
Having named tuples in Kotlin is gonna bring the same benefits, but with much better ergonomics and syntax.
E.g. in Rust, while it's more common to destructure tuples, to access an element you have to do my_tuple.0
or .1
or whatever is the order, and even IDE actions can't suggest proper names in destructuring. Named tuples don't suffer from this issue, but also without requiring you to make an entire class for a single use.