https://kotlinlang.org logo
#library-development
Title
# library-development
s

Sam Stone

09/27/2023, 2:48 PM
Idea: generalized version of
Pair
and
Triple
called
Tuple
that uses code gen to create variables
first
,
second
, etc. (or
ItemN
like in C#), based on the number of arguments passed in. Is this possible? Where would I begin? I have never made a compiler plugin.
e

ephemient

09/27/2023, 6:29 PM
doesn't seem compiler related to me
you need real classes (e.g.
Tuple4
,
Tuple5
, etc.) for them to be passed around
and since you can only have a finite number of classes, you can simply write overloads
Copy code
fun <T1, T2, T3, T4> Tuple(first: t1, second: t2, third: t3, fourth: t4): Tuple4
fun <T1, T2, T3, T4, T5> Tuple(first: t1, second: t2, third: t3, fourth: t4, fifth: t5): Tuple5
...
if you want them all to have a "constructor" named
Tuple
hmm, on further thought, in principle you could have the compiler synthesize types
Tuple<T1, T2, T3, T4>
etc. that are all backed by some internal
class TupleN(data: Array<Any>)
. dunno about it being worthwhile though, since unnamed tuples aren't an encouraged pattern in Kotlin
s

Sam Stone

10/06/2023, 2:46 AM
1. The last solution wouldn't provide type-safe property access. 2. What do you mean by "unnamed tuples"? 3. Why would it need to be backed by an internal class?
a

aru

10/10/2023, 11:25 AM
Isn't that a built-in
data class
feature with
componentN()
functions? https://kotlinlang.org/docs/data-classes.html
s

Sam Stone

10/16/2023, 2:57 AM
@aru 1.
N
only goes up to the 40's as of recent 2. The only way to do it that way is to have many nullable properties, so no type safety.