https://kotlinlang.org logo
#language-proposals
Title
# language-proposals
e

Eduardo Guerra

09/11/2020, 1:47 PM
Hello, everybody! I just arrived at this channel so it's possible that someone has already asked this question before. But, I was wondering if anyone else would like to have n-uples in return statements in kotlin(swift style) like this:
Copy code
func returnMultipleValues() -> (String, Int, Double) {
    return ("Swift Tuple", 12, 3.14)
}
PS. I know how to use lists hahaha, I just think that it would be a lot easier this way
14
1
r

raulraja

09/11/2020, 2:32 PM
I'd love this kind of feature too. It probably requires TupleN or a typelevel HList to represent heterogeneous types. Scala dotty includes now HList and HMap for similar reasons. Also while we are at wishing I'd like auto conversions between TupleN and functions args respecting the types. This type of programs can't be represented with regular lists or arrays without loosing type info so they require compiler support to acknowledge there are generic products and sum types, and potentially a restricted number of max args for Java compat. I think Kotlin had Tuple support at some point but not sure why it was retracted beside the naming mentioned here https://discuss.kotlinlang.org/t/tuples-deprecated/787/5
👏 1
h

henrikhorbovyi

09/11/2020, 2:41 PM
It would be a really cool feature
e

Eduardo Guerra

09/11/2020, 2:43 PM
Exactly! There are lots of alternatives, the one I like the most is
Copy code
data class TwoThings(val id: String, val number: Int)

fun returnTwoThings(): TwoThings {
    // Complex computation...
    return TwoThings(strId, number)
}
But compared to the swift syntax it's really boilerplate, especially if I'm willing to use this combination only once
e

edrd

09/11/2020, 2:51 PM
You could use
Pair
and
Triple
for that:
Copy code
fun returnTwoThings(): Pair<String, Int> {
  return strId to number
}
Which can then be extracted nicely with destructuring:
Copy code
val (first, second) = returnTwoThings()
e

Eduardo Guerra

09/11/2020, 2:55 PM
Sure, That's great, but what if I want four elements, in a return statement it may be difficult to happen but if I want to call a function passing four parameters given by another function it is very easy to happen like differentTypes(givesFourElements())
e

edrd

09/11/2020, 2:57 PM
If you need more than 3 elements and is modelling this with tuples, I think there maybe a design problem in your code. It should probably be a data class instead of a generic structure.
1
e

Eduardo Guerra

09/11/2020, 3:04 PM
As I said before, yes, you could use a data class. I just think it would be nice to have an alternative. But I agree, sometimes it's better to reach for orthogonality in your language rather than convenience. Having too many ways for solve the same problem may be bad. Still, I really enjoy this feature when working with swift.
👍 1
k

kioba

09/11/2020, 5:24 PM
If you need more than 3 elements … there maybe a design problem in your code.
I used to think the same, later I run into problems over and over where generating a new, short lived
data class
was bloating the codebase and implementing a generic TupleN solution was the resolution. I believe there is space for tuples especially areas where we would use them as function arguments.
☝️ 3
✔️ 3
e

Eduardo Guerra

09/11/2020, 5:48 PM
Do you have this tupleN code available somewhere?I'd be glad to take a look.
k

kioba

09/11/2020, 6:02 PM
I would suggest to use the Arrow Core library which contains a great Tuple implementation. You can find it here which supports up to 22 arguments: https://github.com/arrow-kt/arrow-core/blob/master/arrow-core-data/src/main/kotlin/arrow/core/TupleN.kt There is a Tuple generation script if you are looking for a standalone solution, but I would suggest using the Arrow lib, it contains tremendous amount of helper functions and continuous support in the #arrow channel. https://github.com/arrow-kt/arrow/tree/master/generator-scripts
👍 1
❤️ 1
n

Nir

09/13/2020, 2:35 AM
a classic example where, IMHO, you want tuples
is zip
python's zip takes N sequences and returns a sequence over N-tuples
kotlin's zip is really anemic by comparison
I think that while it's often the case you want a data class with named fields, in a language with destructuring like Kotlin, it's weird to get rid of tuples completely
That said I would think in most "non-generic" code, a dataclass is better than a 4+-tuple
a

alexsullivan114

09/16/2020, 7:00 PM
I like the idea of arbitrarily large tuples, but I do feel like there's an alternative, super simple solution - just including a bunch of tuple classes (past 3 I guess) in the standard library.
n

Nir

09/16/2020, 7:02 PM
@alexsullivan114 Yep, I'm with you 100%
up until 9 would cover 99.9999% of use cases
the problem though is that there' sstill tons of boilerplate for actually writing out the functions
for example, you still need 9 overloads of zip
a

alexsullivan114

09/16/2020, 7:02 PM
Oh that's a good point.
n

Nir

09/16/2020, 7:03 PM
Believ eit or not but many many languages just suck it up and do this
Scala, Rust, I believe
a

alexsullivan114

09/16/2020, 7:03 PM
Do they solve that problem in Swift? Like is there any type information for "Arbitrary sized tuple"?
n

Nir

09/16/2020, 7:03 PM
I have no idea! That's a good question. Generally, dealing genercally with tuples required variadic generics, which is a very rare feature
based on a quick google search, I see people proposing it for swift, so I'm going to guess "no"
Obviously, in dynamically typed python, you can just itrate over something that's heterogeneously typed, so you don't have that issue, and you don't need to work about expressing the type signature correctly either.
so you can write
zip
in python, no problem
4 Views