Andrei Marshalov
08/16/2019, 8:40 AMCloseable
interface and copied use
extension from <http://kotlin.io|kotlin.io>
package. It looks like:
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: 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?Artyom Degtyarev [JB]
08/16/2019, 9:13 AM@Throws
-annotated function instead of the lambda.Andrei Marshalov
08/16/2019, 9:20 AMArtyom Degtyarev [JB]
08/19/2019, 3:18 PMAndrei Marshalov
08/20/2019, 7:27 AM