added static List.fill(size) { ... }
This commit is contained in:
1 parent
f1003f5b95
commit
a7ab0d3905
9 files changed
+64
-9
No files matched your search
@@ -4359,6 +4359,7 @@ class Compiler(
|
||||
is ListLiteralRef -> inferListLiteralTypeDecl(ref)
|
||||
is MapLiteralRef -> inferMapLiteralTypeDecl(ref)
|
||||
is ConstRef -> inferTypeDeclFromConst(ref.constValue)
|
||||
is RangeRef -> TypeDecl.Simple("Range", false)
|
||||
is CallRef -> {
|
||||
val targetDecl = resolveReceiverTypeDecl(ref.target) ?: seedTypeDeclFromRef(ref.target)
|
||||
val targetName = when (val target = ref.target) {
|
||||
@@ -4394,6 +4395,7 @@ class Compiler(
|
||||
is ObjString -> TypeDecl.Simple("String", false)
|
||||
is ObjBool -> TypeDecl.Simple("Bool", false)
|
||||
is ObjChar -> TypeDecl.Simple("Char", false)
|
||||
is ObjRange -> TypeDecl.Simple("Range", false)
|
||||
is ObjNull -> TypeDecl.TypeNullableAny
|
||||
is ObjList -> TypeDecl.Generic("List", listOf(TypeDecl.TypeAny), false)
|
||||
is ObjMap -> TypeDecl.Generic("Map", listOf(TypeDecl.TypeAny, TypeDecl.TypeAny), false)
|
||||
@@ -8714,7 +8716,7 @@ class Compiler(
|
||||
startPos = start
|
||||
)
|
||||
val declaredFn = FunctionDeclStatement(spec)
|
||||
if (isStatic) {
|
||||
if (isStatic && parentIsClassBody) {
|
||||
currentInitScope += declaredFn
|
||||
NopStatement
|
||||
} else
|
||||
|
||||
@@ -169,18 +169,29 @@ internal suspend fun executeFunctionDecl(
|
||||
spec.extTypeName?.let { typeName ->
|
||||
val type = scope[typeName]?.value ?: scope.raiseSymbolNotFound("class $typeName not found")
|
||||
if (type !is ObjClass) scope.raiseClassCastError("$typeName is not the class instance")
|
||||
scope.addExtension(
|
||||
type,
|
||||
spec.name,
|
||||
ObjRecord(
|
||||
if (spec.isStatic) {
|
||||
type.createClassField(
|
||||
spec.name,
|
||||
compiledFnBody,
|
||||
isMutable = false,
|
||||
visibility = spec.visibility,
|
||||
declaringClass = null,
|
||||
pos = spec.startPos,
|
||||
type = ObjRecord.Type.Fun,
|
||||
typeDecl = spec.typeDecl
|
||||
)
|
||||
)
|
||||
} else {
|
||||
scope.addExtension(
|
||||
type,
|
||||
spec.name,
|
||||
ObjRecord(
|
||||
compiledFnBody,
|
||||
isMutable = false,
|
||||
visibility = spec.visibility,
|
||||
declaringClass = null,
|
||||
type = ObjRecord.Type.Fun,
|
||||
typeDecl = spec.typeDecl
|
||||
)
|
||||
)
|
||||
}
|
||||
val wrapperName = spec.extensionWrapperName ?: extensionCallableName(typeName, spec.name)
|
||||
val wrapper = ObjExtensionMethodCallable(spec.name, compiledFnBody)
|
||||
scope.addItem(
|
||||
|
||||
@@ -4021,6 +4021,14 @@ class ScriptTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testListFill() = runTest {
|
||||
eval("""
|
||||
val x = List.fill(5) { it*10 }
|
||||
assertEquals( [0,10,20,30,40], x)
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun binarySearchTest() = runTest {
|
||||
eval(
|
||||
|
||||
@@ -422,6 +422,13 @@ fun List<T>.sort(): void {
|
||||
sortWith { a, b -> a <=> b }
|
||||
}
|
||||
|
||||
/* Build a new list of `size` elements by calling `block(index)` for each index. */
|
||||
static fun List<T>.fill(size: Int, block: (Int)->T): List<T> {
|
||||
val result = List<T>()
|
||||
for( i in 0..<size ) result += block(i)
|
||||
result
|
||||
}
|
||||
|
||||
/* Print this exception and its stack trace to standard output. */
|
||||
fun Exception.printStackTrace(): void {
|
||||
println(this)
|
||||
|
||||
Reference in new issue
Block a user