https://kotlinlang.org logo
#announcements
Title
# announcements
t

Travis Griggs

10/28/2019, 5:11 PM
I don’t understand why this works: val UInt.le_ubytes:UByteArray get() { val quad = (0 until 31 step 8 ).map { shift -> (this shr shift and 0xFFu).ubyte } return quad.toUByteArray() } but this doesn’t work: val UInt.le_ubytes:UByteArray get() { val quad = (0 until 31 step 8 ).map { shift -> (this shr shift and 0xFFu).ubyte } return ubyteArrayOf(*quad) } It hates on the ubyteArrayOf() with the spread operator. It wants a UByteArray as the argument. Which is pointless, because why would I be using the ubyteArrayOf() helper if I had a UByteArray already?
e

Evan R.

10/28/2019, 6:37 PM
I believe that’s because
.map()
returns a list, not an array. The spread operator only works on arrays, so the following would work:
Copy code
val UInt.le_ubytes:UByteArray get() {
    val quad = (0 until 31 step 8 ).map { shift -> (this shr shift and 0xFFu).ubyte }
    return ubyteArrayOf(*(quad.toTypedArray()))
   }
1
t

Travis Griggs

10/28/2019, 6:37 PM
thank you. that’s the hidden bit of sauce i was looking for (spread only works on arrays, not lists)