tl;dr we cannot use spread operator with collectio...
# announcements
s
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
Copy code
listOf(a, b, listOf(c, d).toTypedArray())
Even though,
SpreadBuilder.addSpread
supports Collections and Iterators, based on this,
Copy code
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:
Copy code
listOf(a, b, listOf(c,d))
This leads to this in bytecode:
Copy code
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
s
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.
sad
d
Yup