Travis Griggs
02/09/2021, 7:38 PMMarc Knaup
02/09/2021, 7:44 PMJames Richardson
02/09/2021, 7:51 PMTravis Griggs
02/09/2021, 7:53 PMJoel
02/09/2021, 8:01 PMJoel
02/09/2021, 8:02 PMval padded = array + Array(size) { value }
Travis Griggs
02/09/2021, 8:10 PMval padded = original + Array(targetSize - original.size) { value }
Joel
02/09/2021, 8:12 PMoriginal.padded(targetSize) { value }
which may or may not be enough of a distinction versus parenthetical declaration.Travis Griggs
02/09/2021, 8:13 PMdfriehs
02/09/2021, 8:18 PMArray
constructor? Writing the value in curly braces would definitely bring distinction.
inline fun UByteArray.padded(size: UInt, init: (index: Int) -> UByte): UByteArray =
this + Array(size - this.size) { init.invoke(this.size + it) }
Edit: Next time I will refresh messages pre posting.Travis Griggs
02/09/2021, 8:30 PMinline
there? I see it in a lot of the core functions in the docs, but I never do it for any of mine. When should I think about doing that? And what do I get from it? I'm assuming it potentially makes things more optimal. In which case, wouldn't you only add it when you'd proved it was a pain point? Otherwise, why not just put inline everywhere?James Richardson
02/09/2021, 9:06 PMdfriehs
02/09/2021, 9:08 PMinline
to allow for inlining of the init
lambda. The Kotlin documentation suggests inlining functions such as above to circumvent overhead related to higher-order functions - see https://kotlinlang.org/docs/reference/inline-functions.html
Inlining ordinary functions may bring dimnishing (or negative?) returns on performance and bloat code size, but with very small higher-order convenience functions inlining can make sense.Joel
02/09/2021, 9:11 PMinline fun UByteArray.padded(size: UInt, init: (index: Int) -> UByte) = this + Array(size - this.size, init)
Starting to feel more like a C macro 🤣dfriehs
02/09/2021, 9:15 PMinit
lambda directly, init
's index parameter is not the index of the element in the resulting array, which may lead to confusion.Joel
02/09/2021, 9:16 PM