Kotlin Array problems and suggestions


#1

Hi there,

I have a problem and a sugestion to API doc.

  1. Why the following code failed to compile on last statement with Array.toList() method?

import java.util.Date fun main(args : Array<String>) {   val a1 : Array<Date> = array(Date(), Date())   println(a1.toList()) // okay   val a2 : Array<out Any> = a1   print(a2.toList())   // failed? }

output>
ERROR: C:appskotlinctest.kt: (6, 9) Type mismatch: inferred type is jet.Array<out jet.Any> but jet.Array<jet.Any> was expected


2) When reading the Kotlin API doc, I can’t see navigation link to “jet.Arrary”, but it does shows as part of some method signature. Despite I find the hidden linke such as this: http://jetbrains.github.com/kotlin/versions/snapshot/apidocs/jet/Array.html but I don’t see “toList()” method there. Nor I see the constructor method that allow you to initialize array. (eg: Array<Any?>(3) { i -> null }) Are these intended to be hidden from users? I suggest these to be clearly documented in API doc.

  1. Also, The current Array.toString() just shows same thing as the Java’s array, which is not very convenient for inspection. I much prefer it to show content just as the ArrayList would but with “[]” insted of “{}” maybe. This is a reason I call “.toList()” wich is most of the time for debug/logging purpose only. Is this something you guys would consider adding as default in “jet.Array”?

#2

toList() is documented here http://jetbrains.github.com/kotlin/versions/snapshot/apidocs/kotlin/jet/Array-extensions.html#toList() and it expects <T> and not <out T>

I wouldn’t use declaration-site variance in var/val definitions, use type casts instead.

``

val a2 : Array<Any> = a1 as Array<Any>


#3

Thanks Daniel, however that solution generates a warning. How would I get rid of this warning?

``

fun main(args : Array<String>) {
  val a1 : Array<String> = array(“one”, “two” , “three”)
  val a2 : Array<Any> = a1 as Array<Any>
  print(a2.toList())
}

compiler output>
WARNING: /Users/zemian/apps/kotlinc/test3.kt: (3, 28) This cast can never succeed

Also, even if we have other workarounds, to me, this Array#toList() is dangerous to use because the compiler didn’t error out when it compile this line “val a2 : Array<out Any> = a1” but only on calling a2.toList() it failed. I would prefer compiler doesn’t allow the assignment in the first place, or find a way to make Array#toList() not to fail. Because the “a2” is legit (I can use other methods) except calling toList().

Zemian


#4

The compilation error you are getting is a bug in type inference. Can be worked around byt specifying type arguments explicitly. KDoc needs to be improved a lot. Feel free to contribute.


#5

Thanks Andrey,

The compilation error you are getting is a bug in type inference.

Is there a bug report already or you want me to help create one?

Can be worked around byt specifying type arguments explicitly.

Can you please show me how?


#6

I searched the YouTrack and didn't find an issue match this, so I help created one: http://youtrack.jetbrains.com/issue/KT-3213