Difference between ByteArray and Array<Byte>


#1

I don't understand why e.g. the java.security.MessageDigest.digest() method which is declared as returning "byte[]" in Java returns a "ByteArray" in Kotlin although the Kotlin examples usually use "Array<Byte>".

E.g. the following does not work:

fun main(args : Array<String>) {

  val md = java.security.MessageDigest.getInstance(“SHA”)

  if (md == null) throw NullPointerException()

  val result : Array<Byte>? = md.digest()

}

Type mismatch: inferred type is ByteArray? but Array? was expected

How should I covert those two?


#2

I don't understand why e.g. the java.security.MessageDigest.digest() method which is declared as returning "byte[]" in Java returns a "ByteArray" in Kotlin although the Kotlin examples usually use "Array<Byte>".

Please, see my answer here: http://stackoverflow.com/questions/9457942/difference-between-bytearray-and-arraybyte-in-kotlin/9462835#9462835

On the conversion part: we should add conversion functions to the standard library. Do you mind filing an issue about it? Thanks.


#3

The type system would be easier to deal with if you only had to work with Array<Byte> types.  I would be nice if the compiler auto converted those to the specialized types and hid that from the user.  If the user really wanted a non-specialized, boxed array type then he should be using nullable type perhaps like Array<Byte?>.


#4

This is what we started with. Unfortunately, it doesn't play well with generics.


#5

Yeah guess you'd have to be able generate specialized implemenations for all generic classes too automatically if you also want to avoid autoboxing in those cases also.  Is that what your refering too?


#6

Basically, yes.