https://kotlinlang.org logo
Title
s

sikri

11/26/2019, 10:43 AM
tl;dr we cannot use spread operator with collections, even though SpreadBuilder supports collections and iterator under the neath. Will it change anywhen? For now, I have to write
listOf(a, b, listOf(c, d).toTypedArray())
Even though,
SpreadBuilder.addSpread
supports Collections and Iterators, based on this,
public void addSpread(Object container) {
        ...
        else if (container instanceof Collection) {
            list.addAll((Collection) container);
        }
        else if (container instanceof Iterable) {
            for (Object element : (Iterable) container) {
                list.add(element);
            }
        }
        ...
    }
I cannot write code like this, even though it seems being supported:
listOf(a, b, listOf(c,d))
This leads to this in bytecode:
L41
    LINENUMBER 154 L41
    ALOAD 15
    ICONST_0
    ANEWARRAY pl/some/type
    INVOKEINTERFACE java/util/Collection.toArray ([Ljava/lang/Object;)[Ljava/lang/Object; (itf)
   L43
    ASTORE 21
    ALOAD 13
    ALOAD 12
    ALOAD 21
    INVOKEVIRTUAL kotlin/jvm/internal/SpreadBuilder.addSpread (Ljava/lang/Object;)V
And as you can see, there is a useless array copy
d

diesieben07

11/26/2019, 10:46 AM
s

sikri

11/26/2019, 10:48 AM
4 Jul 2016 16:18
I suppose it might be sufficient to start with adding spread support only for Collection and to postpone spread-convention until it is proved to be useful for other use-cases.
😒ad:
d

diesieben07

11/26/2019, 10:48 AM
Yup