2

Just wondering, what's the difference between to- and as- prefixes the Kotlin libraries?

var buffer:ByteArray
// what's the difference between these two
buffer.toUByteArray()
buffer.asUByteArray()

I would imagine one refers to a copy of the array, and the other is more of a wrapper pointing to the same values in memory, but I'm not sure.

1 Answer 1

2

toUByteArray: Returns an array of type UByteArray, which is a copy of this array where each element is an unsigned reinterpretation of the corresponding element of this array.

asUByteArray: Returns an array of type UByteArray, which is a view of this array where each element is an unsigned reinterpretation of the corresponding element of this array.

Explanation about difference between in copy and view:

While executing the functions, some of them return a copy of the input array, while some return the view. When the contents are physically stored in another location, it is called Copy. If on the other hand, a different view of the same memory content is provided, we call it as View

References:

toUByteArray

asUByteArray

NumPy - Copies & Views

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.