+inference bugs

+List.ensureCapacity
optimized List.fill
This commit is contained in:
sergeych committed 2026-04-20 13:56:47 +03:00
1 parent 6ba128f7ba
commit 87060c7eb7
5 files changed
+49 -6

No files matched your search

@@ -3424,6 +3424,10 @@ class Compiler(
index < expectedParams.size
) {
expectedParams[index]
} else if (param.type == TypeDecl.TypeAny && index == 0 && argsDeclaration.params.size == 1 &&
implicitItType != null
) {
implicitItType
} else {
param.type
}
@@ -528,6 +528,22 @@ open class ObjList(initialList: MutableList<Obj> = mutableListOf()) : Obj() {
} else self.list.removeLast()
self
}
addFnDoc(
name= "ensureCapacity",
doc = """
ensure the list capacity allows storing specified amount if items without reallocation.
If current capacity is greater or equal to `count`, does nothing. Note that possible reallocation
could be a costly operation,
""".trimIndent(),
params = listOf(ParamDoc("count", type("lyng.Int"))),
moduleName = "lyng.stdlib"
) {
val self = thisAs<ObjList>()
val list = self.list as ArrayList
val count = requireOnlyArg<ObjInt>().value.toInt()
list.ensureCapacity(count)
self
}
addFnDoc(
name = "removeRange",
@@ -436,4 +436,12 @@ class StdlibTest {
assertEquals(2, x)
""".trimIndent())
}
@Test
fun testListFill() = runTest {
eval("""
val x = List.fill(5) { i -> i * 10 }
assertEquals([0, 10, 20, 30, 40], x)
""".trimIndent())
}
}