Fix array init and add listOf(). (#109)

This commit is contained in:
Nikolay Igotti
2016-12-02 13:18:57 +03:00
committed by GitHub
parent 35dfb35246
commit 83dcb0fc3a
7 changed files with 23 additions and 14 deletions
@@ -36,6 +36,7 @@ class KonanBuiltIns: KotlinBuiltIns {
object KonanPlatform : TargetPlatform("Konan") { object KonanPlatform : TargetPlatform("Konan") {
override val defaultImports: List<ImportPath> = Default.defaultImports + listOf( override val defaultImports: List<ImportPath> = Default.defaultImports + listOf(
ImportPath("kotlin.*"), ImportPath("kotlin.*"),
ImportPath("kotlin.collections.*"),
ImportPath("kotlin.io.*"), ImportPath("kotlin.io.*"),
ImportPath("konan.*") ImportPath("konan.*")
) )
+1 -1
View File
@@ -478,7 +478,7 @@ task array_list1(type: RunKonanTest) {
} }
task listof0(type: RunKonanTest) { task listof0(type: RunKonanTest) {
goldValue = "abc\n" goldValue = "abc\n[a, b, c, d]\n[n, s, a]\n"
source = "runtime/collections/listof0.kt" source = "runtime/collections/listof0.kt"
} }
@@ -2,4 +2,9 @@ fun main(args : Array<String>) {
val list = arrayListOf("a", "b", "c") val list = arrayListOf("a", "b", "c")
for (element in list) print(element) for (element in list) print(element)
println() println()
list.add("d")
println(list.toString())
val list2 = listOf("n", "s", "a")
println(list2.toString())
} }
+1 -2
View File
@@ -10,8 +10,7 @@ public final class String : Comparable<String>, CharSequence {
@SymbolName("Kotlin_String_hashCode") @SymbolName("Kotlin_String_hashCode")
external public override fun hashCode(): Int external public override fun hashCode(): Int
// TODO: make it Any? public operator fun plus(other: Any?): String {
public operator fun plus(other: Any): String {
return plusImpl(other.toString()) return plusImpl(other.toString())
} }
@@ -149,7 +149,6 @@ class ArrayList<E> private constructor(
} }
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
//TODO: rethink instance checks, shall it be other is List<*>?
return other === this || return other === this ||
(other is List<*>) && contentEquals(other) (other is List<*>) && contentEquals(other)
} }
@@ -6,9 +6,9 @@ package kotlin.collections
* either throwing exception or returning some kind of implementation-specific default value. * either throwing exception or returning some kind of implementation-specific default value.
*/ */
fun <E> arrayOfLateInitElements(size: Int): Array<E> { fun <E> arrayOfLateInitElements(size: Int): Array<E> {
// TODO: maybe use an empty array, fix array creation. // TODO: maybe use an empty array.
// return (if (size == 0) emptyArray else Array<Any>(size)) as Array<E> // return (if (size == 0) emptyArray else Array<Any>(size)) as Array<E>
return Array<Any>(size) as Array<E> return Array<E>(size)
} }
/** /**
@@ -17,7 +17,7 @@ fun <E> arrayOfLateInitElements(size: Int): Array<E> {
* either throwing exception or returning some kind of implementation-specific default value. * either throwing exception or returning some kind of implementation-specific default value.
*/ */
fun <E> Array<E>.copyOfLateInitElements(newSize: Int): Array<E> { fun <E> Array<E>.copyOfLateInitElements(newSize: Int): Array<E> {
val result = Array<Any>(newSize) as Array<E> val result = Array<E>(newSize)
this.copyRangeTo(result, 0, if (newSize > this.size) this.size else newSize, 0) this.copyRangeTo(result, 0, if (newSize > this.size) this.size else newSize, 0)
return result return result
} }
@@ -23,9 +23,17 @@ public interface MutableIterable<out T> : Iterable<T> {
override fun iterator(): MutableIterator<T> override fun iterator(): MutableIterator<T>
} }
fun <E> Array<E>.asList(): List<E> {
// TODO: consider making lighter list over an array.
val result = ArrayList<E>(this.size)
for (e in this) {
result.add(e)
}
return result
}
public fun <T> arrayListOf(vararg args: T): MutableList<T> { public fun <T> arrayListOf(vararg args: T): MutableList<T> {
// TODO: fix me! val result = ArrayList<T>(args.size)
val result = ArrayList<Any>(args.size) as ArrayList<T>
for (arg in args) { for (arg in args) {
result.add(arg) result.add(arg)
} }
@@ -33,10 +41,7 @@ public fun <T> arrayListOf(vararg args: T): MutableList<T> {
} }
/* /*
* FIXME: Suggested code from @olonho is following * TODO: in Big Kotlin this function is following: (see libraries/stdlib/src/kotlin/collections/Collections.kt)
*
* public fun <T> listOf(element: T): List<T> = arrayListOf(element)
*
* but in Big Kotlin this function is following: (see libraries/stdlib/src/kotlin/collections/Collections.kt)
* public fun <T> listOf(vararg elements: T): List<T> = if (elements.size > 0) elements.asList() else emptyList() * public fun <T> listOf(vararg elements: T): List<T> = if (elements.size > 0) elements.asList() else emptyList()
*/ */
public fun <T> listOf(vararg args: T): List<T> = args.asList()