Strange think in stdlib-common I found, looks like...
# multiplatform
t
Strange think in stdlib-common I found, looks like a bug. Details in comments. (
setOf(singleElement)
)
Copy code
/**
 * 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()
Copy code
/**
 * 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... ?
l
What is the issue you are seeing? Looks fine to me
k
cycle?
t
Cycle indeed, infinite recursion.
m
There is another function in jvm and native source-set which takes only one element
Copy code
public fun <T> setOf(element: T): Set<T>
I think this function is being used instead of one with the vararg
t
@Mayank Shouldn't then there be an except declaration in common?
m
As per my understanding, many files in the stdlib are generated including the
_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)
as both the methods are in the same package, imports need not be changed
👍 1