https://kotlinlang.org logo
#atrium
Title
# atrium
a

Anes

07/20/2020, 5:29 PM
Hi, I am working on this issue [479] and I need some help implementing
toVarArg
method it should accept a
IterableLike<T>
which is
Any
, then I need to check its type and return
Pair<T, Array<out T>>
my current solution looks like this (below 👇), I want to know if I am on the right path or I need to rethink this solution, any tips would be appreciated. Thank you for your time.
Copy code
inline fun <reified T> toVarArg(iterable: IterableLike<T>): Pair<T, Array<out T>> {
    when (iterable) {
        is Iterable<*> -> {
            val mappedIterable = iterable.map { it as T }.asIterable()
            return mappedIterable.first() to mappedIterable.drop(1).toTypedArray()
        }
        is Sequence<*> -> {
            val mappedIterable = iterable.map { it as T }.asIterable()
            return mappedIterable.first() to mappedIterable.drop(1).toTypedArray()
        }
        is Array<*> -> {
            val mappedIterable = iterable.map { it as T }.asIterable()
            return mappedIterable.first() to mappedIterable.drop(1).toTypedArray()
        }
        is ByteArray -> {
            val mappedIterable = iterable.map { it as T }.asIterable()
            return mappedIterable.first() to mappedIterable.drop(1).toTypedArray()
        }
        is CharArray -> {
            val mappedIterable = iterable.map { it as T }.asIterable()
            return mappedIterable.first() to mappedIterable.drop(1).toTypedArray()
        }
        is ShortArray -> {
            val mappedIterable = iterable.map { it as T }.asIterable()
            return mappedIterable.first() to mappedIterable.drop(1).toTypedArray()
        }
        is IntArray -> {
            val mappedIterable = iterable.map { it as T }.asIterable()
            return mappedIterable.first() to mappedIterable.drop(1).toTypedArray()
        }
        is LongArray -> {
            val mappedIterable = iterable.map { it as T }.asIterable()
            return mappedIterable.first() to mappedIterable.drop(1).toTypedArray()
        }
        is FloatArray -> {
            val mappedIterable = iterable.map { it as T }.asIterable()
            return mappedIterable.first() to mappedIterable.drop(1).toTypedArray()
        }
        is DoubleArray -> {
            val mappedIterable = iterable.map { it as T }.asIterable()
            return mappedIterable.first() to mappedIterable.drop(1).toTypedArray()
        }
        is BooleanArray -> {
            val mappedIterable = iterable.map { it as T }.asIterable()
            return mappedIterable.first() to mappedIterable.drop(1).toTypedArray()
        }
        else -> throw IllegalArgumentException("toVarArg accepts arguments of types Iterable, Sequence, Array")
    }
r

robstoll

07/20/2020, 7:48 PM
Can you please create a WIP/draft pull request. That's always easier for me to give feedback. Thanks
By a glance I can say that there is too much code duplication and I would avoid the mapping. If we deal with a wrong element type, then the assertion will/should fail. Convert to an Iterable if necessary and call one private fun which deals with Iterable (this one should already exist)
a

Anes

07/21/2020, 7:57 AM
thanks for the help, I will make the changes and create a WIP pull request
3 Views