any way i can get around having to bloat all my cl...
# getting-started
h
any way i can get around having to bloat all my classes as so for others' convenience:
fun foo(values: DoubleArray) = //do the things
fun foo(vararg values: Double) = foo(DoubleArray(values.size) { values[it] })
fun foo(values: List<Double>) = foo(values.toDoubleArray())
fun foo(values: Vector) = foo(values.toDoubleArray())
I can use extension methods and chuck many of them into separate files, but sometimes I can't, like in the case of inner classes or objects. is there a better way that i'm not really seeing? afaik there is absolutely no shared parent class between arrays and lists. my
Vector
class at least shares
Iterable<Double>
with
List<Double>
, but in many cases it's more efficient to duplicate the body of functions the two share instead of unifying them under
Iterable
because most methods require
Iterable::toList
before I can do
foo
things to it.
I could have my
Vector
class implement
List<Double>
instead of
Iterable<Double>
, but I specifically chose to not do that, because a Vector is not a List
Oh, I might make it a collection
k
I'd say just pick one and let users convert, is it really going to be common to mix arrays and lists, and varargs?
h
i can't imagine so, but i like the idea of always programming whatever i'm doing at the moment in such a way it makes the next step easier
k
I feel obligated to post this link now simple smile https://en.m.wikipedia.org/wiki/You_aren%27t_gonna_need_it
👍 1
h
hmmmm
k
But to actually answer your question: no, there's nothing in Kotlin that helps with this. The stdlib even resorts to code generation for array and iterable overloads. There's a popular KEEP that would help with this: https://github.com/Kotlin/KEEP/pull/87
h
thank you