Tomasz Krakowiak
09/06/2021, 2:10 AMsetOf(singleElement)
)Tomasz Krakowiak
09/06/2021, 2:11 AM/**
* Returns a new read-only set with the given elements.
* Elements of the set are iterated in the order they were specified.
* The returned set is serializable (JVM).
* @sample samples.collections.Collections.Sets.readOnlySet
*/
public fun <T> setOf(vararg elements: T): Set<T> = if (elements.size > 0) elements.toSet() else emptySet()
/**
* Returns a [Set] of all elements.
*
* The returned set preserves the element iteration order of the original array.
*/
public fun <T> Array<out T>.toSet(): Set<T> {
return when (size) {
0 -> emptySet()
1 -> setOf(this[0])
else -> toCollection(LinkedHashSet<T>(mapCapacity(size)))
}
}
How, the heck is it supposed to work for array of size 1? ; p I guess on runtime we never actually use both of those implementation... ?Luis
09/06/2021, 2:18 AMkqr
09/06/2021, 6:20 AMTomasz Krakowiak
09/06/2021, 6:21 AMMayank
09/06/2021, 6:47 AMpublic fun <T> setOf(element: T): Set<T>
I think this function is being used instead of one with the varargTomasz Krakowiak
09/06/2021, 7:19 AMMayank
09/06/2021, 7:33 AM_Arrays.kt
and due to method overloading, platform specific setOf(element: T)
is used if present otherwise it fall backs to setOf(vararg elements: T)
Mayank
09/06/2021, 7:34 AM