You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
val standardKdoc ="Returns an immutable list that wraps the same backing array without copying the elements."
43
+
44
+
for (baseType inBaseType.entries) {
45
+
function(
46
+
kdoc =when (baseType) {
47
+
GENERIC-> standardKdoc
48
+
else-> {
49
+
"""
50
+
$standardKdoc
51
+
52
+
Note that accessing values from the resulting list will auto-box them everytime they are accessed. This is because [${baseType.generatedClassName}] stores primitive values whereas [List] is defined as a generic type. If the number of accesses is expected to be multiple times larger than the size of this array, then you might want to consider using [toList] instead in order to copy all the elements into a standalone list and only auto-box each element once.
// IMPORTANT: Don't attempt to delegate to the backing array (eg. "return values.asList()") because that can allow an outsider to mutate the backing array via the list wrapper
64
+
// See https://youtrack.jetbrains.com/issue/KT-70779/Array.asList-exposes-mutation-back-door
65
+
addCode(
66
+
"""
67
+
return object : %T<%T>(), %T {
68
+
override val size: Int get() = this@asList.size
69
+
override fun isEmpty(): Boolean = this@asList.isEmpty()
70
+
override fun contains(element: %T): Boolean = this@asList.contains(element)
71
+
override fun get(index: Int): %T = this@asList[index]
72
+
override fun indexOf(element: %T): Int = this@asList.indexOf(element)
73
+
override fun lastIndexOf(element: %T): Int = this@asList.lastIndexOf(element)
74
+
}
75
+
""".trimIndent(),
76
+
AbstractList::class.asTypeName(),
77
+
baseType.type,
78
+
RandomAccess::class.asTypeName(),
79
+
baseType.type,
80
+
baseType.type,
81
+
baseType.type,
82
+
baseType.type,
83
+
)
84
+
}
85
+
}
86
+
}
87
+
40
88
privatefun FileSpec.Builder.addContains() {
41
89
for (baseType inBaseType.entries) {
42
90
function(
@@ -192,6 +240,7 @@ private fun FileSpec.Builder.addSortedDescending() {
GENERIC->"Wraps the backing array in a class that implements the read-only [List] interface by referencing the same backing array without copying the elements."
228
-
else-> {
229
-
"""
230
-
Wraps the backing array in a class that implements the read-only [List] interface by referencing the same backing array without copying the elements.
231
-
232
-
Note that [${baseType.generatedClassName}] stores primitive values whereas [List] operates on generic types so this will auto-box the value that is accessed on every access. If the total number of accesses is expected to be multiple times larger than the total number of elements then you might want to consider converting it into a list instead as that will auto-box all the elements only once at the cost of allocating a separate backing array.
0 commit comments