How do you guys solve function overloads with diff...
# announcements
l
How do you guys solve function overloads with different generics? Is there any way of doing this without creating one overload for every possibility?
Copy code
fun foo<A>(a: A) {}
fun foo<A, B>(a: A, b: B){}
fun foo<A,B,C>(a: A, b: B, c: C) { }
...
until foo<125> types
t
I guess the best way would be to generate those methods instead of writing them all. I've seen something like that in GNU Trove4j : they generate collections for primitive types using code templates, replacing some variables with each primitive type. The generated files are then packed into a library Jar. For what use case do you need so many arguments ? Data-driven tests ?
l
Exactly
Rewriting some portion of the code at #C18FWPKKL
n
I wrote an HList type for holding lists of values of heterogeneous types: sealed class HList object Empty : HList data class HCons<T, Rest:HList>(val value: T, val rest: Rest)
👍 1
Unfortunately at the time, pushing more than a few elements would make the type inference crash the compiler. But kotlinc may be more robust now
d
Is there a best practice for generating this sort of code? Something like KotlinPoet+kapt?
n
i would generate this code using kotlinpoet in gradle
builSrc
and call it in
compileKotlin.doFirst
or a task that compileKotlin depends on also make sure its added as a sourceSet and optionally marked as generated sourceset in idea
if you want ot make this reusable a gradle plugin or kapt make sense
k
@natpryce Looks a lot like how lists are defined in functional programming, interesting!
d
@natpryce I tried the same. Even managed to crash IntelliJ