Make Array<T>(size, init) a constructor of Array
It's not marked as inline, this is why 'crossinline' was added in jaggedArray.kt/jaggedDeep.kt. Will be fixed in the following commits
This commit is contained in:
@@ -2,18 +2,6 @@ package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
// TODO: @library("arrayFromFun")
|
||||
/**
|
||||
* Returns an array with the specified [size], where each element is calculated by calling the specified
|
||||
* [init] function. The `init` function returns an array element given its index.
|
||||
*/
|
||||
public inline fun <reified T> Array(size: Int, init: (Int) -> T): Array<T> {
|
||||
val result = arrayOfNulls<T>(size)
|
||||
for (i in 0..size - 1)
|
||||
result[i] = init(i)
|
||||
return result as Array<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an empty array of the specified type [T].
|
||||
*/
|
||||
|
||||
@@ -28,26 +28,26 @@ public final class StandardClassesTest extends SingleFileTranslationTest {
|
||||
fooBoxTest();
|
||||
}
|
||||
|
||||
|
||||
public void testArrayAccess() throws Exception {
|
||||
fooBoxTest();
|
||||
}
|
||||
|
||||
|
||||
public void testArrayIsFilledWithNulls() throws Exception {
|
||||
fooBoxTest();
|
||||
}
|
||||
|
||||
|
||||
public void testArrayFunctionConstructor() throws Exception {
|
||||
fooBoxTest();
|
||||
}
|
||||
|
||||
|
||||
public void testArraySize() throws Exception {
|
||||
fooBoxTest();
|
||||
}
|
||||
|
||||
public void testArrayConstructorsWithLambda() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
|
||||
//TODO: this feature in not supported for some time
|
||||
//TODO: support it. Probably configurable.
|
||||
// (expected = JavaScriptException.class)
|
||||
|
||||
+15
-7
@@ -21,14 +21,16 @@ import com.google.dart.compiler.backend.js.ast.JsArrayAccess;
|
||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
||||
import org.jetbrains.kotlin.builtins.PrimitiveType;
|
||||
import org.jetbrains.kotlin.js.patterns.DescriptorPredicate;
|
||||
import org.jetbrains.kotlin.js.patterns.NamePredicate;
|
||||
import org.jetbrains.kotlin.js.translate.context.Namer;
|
||||
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
|
||||
import org.jetbrains.kotlin.js.translate.intrinsic.functions.basic.FunctionIntrinsic;
|
||||
import org.jetbrains.kotlin.js.patterns.DescriptorPredicate;
|
||||
import org.jetbrains.kotlin.js.patterns.NamePredicate;
|
||||
import org.jetbrains.kotlin.name.Name;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static com.intellij.openapi.util.text.StringUtil.decapitalize;
|
||||
@@ -40,6 +42,7 @@ public final class ArrayFIF extends CompositeFIF {
|
||||
private static final NamePredicate CHAR_ARRAY;
|
||||
private static final NamePredicate BOOLEAN_ARRAY;
|
||||
private static final NamePredicate LONG_ARRAY;
|
||||
private static final NamePredicate ARRAY;
|
||||
private static final NamePredicate ARRAYS;
|
||||
private static final DescriptorPredicate ARRAY_FACTORY_METHODS;
|
||||
|
||||
@@ -54,7 +57,7 @@ public final class ArrayFIF extends CompositeFIF {
|
||||
arrayFactoryMethodNames.add(Name.identifier(decapitalize(arrayTypeName.asString() + "Of")));
|
||||
}
|
||||
|
||||
Name arrayName = Name.identifier("Array");
|
||||
Name arrayName = KotlinBuiltIns.FQ_NAMES.array.shortName();
|
||||
Name booleanArrayName = PrimitiveType.BOOLEAN.getArrayTypeName();
|
||||
Name charArrayName = PrimitiveType.CHAR.getArrayTypeName();
|
||||
Name longArrayName = PrimitiveType.LONG.getArrayTypeName();
|
||||
@@ -63,6 +66,7 @@ public final class ArrayFIF extends CompositeFIF {
|
||||
CHAR_ARRAY = new NamePredicate(charArrayName);
|
||||
BOOLEAN_ARRAY = new NamePredicate(booleanArrayName);
|
||||
LONG_ARRAY = new NamePredicate(longArrayName);
|
||||
ARRAY = new NamePredicate(arrayName);
|
||||
|
||||
arrayTypeNames.add(charArrayName);
|
||||
arrayTypeNames.add(booleanArrayName);
|
||||
@@ -123,10 +127,14 @@ public final class ArrayFIF extends CompositeFIF {
|
||||
add(pattern(ARRAYS, "set"), SET_INTRINSIC);
|
||||
add(pattern(ARRAYS, "<get-size>"), LENGTH_PROPERTY_INTRINSIC);
|
||||
add(pattern(ARRAYS, "iterator"), new KotlinFunctionIntrinsic("arrayIterator"));
|
||||
add(pattern(NUMBER_ARRAY, "<init>"),new KotlinFunctionIntrinsic("numberArrayOfSize"));
|
||||
add(pattern(CHAR_ARRAY, "<init>"), new KotlinFunctionIntrinsic("charArrayOfSize"));
|
||||
add(pattern(BOOLEAN_ARRAY, "<init>"), new KotlinFunctionIntrinsic("booleanArrayOfSize"));
|
||||
add(pattern(LONG_ARRAY, "<init>"), new KotlinFunctionIntrinsic("longArrayOfSize"));
|
||||
|
||||
add(pattern(NUMBER_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("numberArrayOfSize"));
|
||||
add(pattern(CHAR_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("charArrayOfSize"));
|
||||
add(pattern(BOOLEAN_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("booleanArrayOfSize"));
|
||||
add(pattern(LONG_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("longArrayOfSize"));
|
||||
|
||||
add(pattern(ARRAYS, "<init>(Int,Function1)"), new KotlinFunctionIntrinsic("arrayFromFun"));
|
||||
|
||||
add(ARRAY_FACTORY_METHODS, ARRAY_INTRINSIC);
|
||||
}
|
||||
}
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val s = Array<String>(3) { it.toString() }
|
||||
if (s.size != 3) return "Fail Array size: ${s.size}"
|
||||
if (s[1] != "1") return "Fail Array value: ${s[1]}"
|
||||
|
||||
val i = IntArray(3) { it }
|
||||
if (i.size != 3) return "Fail IntArray size: ${i.size}"
|
||||
if (i[1] != 1) return "Fail IntArray value: ${i[1]}"
|
||||
|
||||
val c = CharArray(3) { it.toChar() }
|
||||
if (c.size != 3) return "Fail CharArray size: ${c.size}"
|
||||
if (c[1] != 1.toChar()) return "Fail CharArray value: ${c[1]}"
|
||||
|
||||
val b = BooleanArray(3) { true }
|
||||
if (b.size != 3) return "Fail BooleanArray size: ${b.size}"
|
||||
if (b[1] != true) return "Fail BooleanArray value: ${b[1]}"
|
||||
|
||||
val l = LongArray(3) { it.toLong() }
|
||||
if (l.size != 3) return "Fail LongArray size: ${l.size}"
|
||||
if (l[1] != 1L) return "Fail LongArray value: ${l[1]}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user