We use the staticCFunction{..} helper function from Kotlin/Native to wrap a Kotlin lambda function into a C function pointer. It only allows having unbound and non-capturing lambda functions.
Could someone elaborate on what it means by
unbound and non-capturing lambda functions
. Does it mean that for instance if you want to register callbacks from c they cannot be in a kotlin class, but rather only top-level function?
k
Kris Wong
01/08/2020, 8:50 PM
i'm not clear on unbound, but non-capturing means that it does not contain references to the context in which it is used
b
Big Chungus
01/08/2020, 8:58 PM
So does that mean there's no way to wrap GTK3 C callbacks in classes for reusability? Even something as a pair of click count buttons managed by Wrapper class?
Big Chungus
01/08/2020, 9:02 PM
@msink found this snippet in your libui
Copy code
/** Function to be run when the user makes a change to the TextField.
* Only one function can be registered at a time. */
fun action(block: TextField.() -> Unit) {
action = block
uiEntryOnChanged(ptr, staticCFunction { _, ref ->
with(<http://ref.to|ref.to><TextField>()) {
action?.invoke(this)
}
}, ref.asCPointer())
}
internal var action: (TextField.() -> Unit)? = null
Can you explain why the compiler does not complain that action is outside the function?
k
Kris Wong
01/08/2020, 9:02 PM
they can exist within a class but can't capture any reference to the class