Idea: generalized version of `Pair` and `Triple` c...
# library-development
s
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
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
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
Isn't that a built-in
data class
feature with
componentN()
functions? https://kotlinlang.org/docs/data-classes.html
s
@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.