Hi, i’m using kotlin-multiplatform plugin to creat...
# kotlin-native
a
Hi, i’m using kotlin-multiplatform plugin to create ios framework and android aar library. Since i use interop with c-libraries in my project i need to do some special stuff at the end of using my objects: call function, that will free memory, allocated by c-functions. To be sure that memory is always freed (eg if an exception is thrown) i added my own
Closeable
interface and copied
use
extension from
<http://kotlin.io|kotlin.io>
package. It looks like:
Copy code
interface Closeable {

    fun close()
}

@Throws(Throwable::class)
inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
    var exception: Throwable? = null
    try {
        return block(this)
    } catch (e: Throwable) {
        exception = e
        throw e
    } finally {
        when {
            this == null -> {}
            exception == null -> close()
            else ->
                try {
                    close()
                } catch (closeException: Throwable) {
                    // cause.addSuppressed(closeException) // ignored here
                }
        }
    }
}
It works fine for android, but in Swift i have some problem. Function signature from xCode:
Copy code
UtilitiesKt.use(receiver: Closeable?, block: (Closeable?) -> Any?, result: AutoreleasingUnsafeMutablePointer<AnyObject??)
use
function in xCode marked as
trowable
, due to
@Throws(Throwable::class)
annotation, but if i try use
trowable
functions in
block
lambda, xCode says:
Invalid conversion from throwing function of type '(Closeable?) throws -> Any?' to non-throwing function type '(Closeable?) -> Any?'
Maybe someone know, how can i mark lambda parameter as
throwable
in kotlin?
a
Hello! This approach is unavailable for now. You can use an interface with a
@Throws
-annotated function instead of the lambda.
a
Thank you, will think about it. Are there any plans to add such approach?
a
Sorry for a long silence. Unfortunately, it is not among the upcoming features. But as far as I know, there are some ideas on implementation.
a
Thank you for reply!