https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
e

Edoardo Luppi

11/06/2023, 10:57 AM
Context receivers seem to be extremely useful when dealing with transformations that should be done under a
memscoped
block.
Copy code
context (NativePlacement)
internal fun LPWSTR.toLatin1Buffer(): LPSTR {
  val latin1BufferSize = WideCharToMultiByte(28591u /* ISO-8859-1 */, 0u, this@toLatin1Buffer, 0, null, 0, null, null)
  val latin1buffer = allocArray<CHARVar>(latin1BufferSize)
  WideCharToMultiByte(28591u /* ISO-8859-1 */, 0u, this@toLatin1Buffer, 0, latin1buffer, 0, null, null)
  return latin1buffer
}
o

Oleg Yukhnevich

11/06/2023, 1:51 PM
IMO, yes and no, because you will always think, that you can easily/safely move
NativePlacement
to context receiver and then, in this case you can accidentally be trapped in a situation when you create single
MemScope
block for whole application - and so it will be never actually cleared So, that' a good use case, but for me it's dangerous - but may be Im overreacting 🙂