From K/N CInterop guide on kotlin webpage: ```We u...
# kotlin-native
b
From K/N CInterop guide on kotlin webpage:
Copy code
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
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
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?
@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
they can exist within a class but can't capture any reference to the class
b
Also how can I obtain the referenced value from
COpaquePointer
as:
Copy code
<http://ref.to|ref.to><TextField>()
Nvm, figured it out:
Copy code
ref.asStableRef<TextField>().get()