kotlin.error
, in the standard library’s Preconditions.kt
, throws an IllegalStateException
. It’s very helpful to use. Meanwhile, AnkoLogger.error
is a wrapper for Android’s Log.e
. I’m finding that I really want to do both of these things at the same time.
My workaround is to have this:
import org.jetbrains.anko.AnkoLogger
import org.jetbrains.anko.error
/**
* Merge together the functionality of AnkoLogger.error and Kotlin's intrinsic error function,
* which throws an IllegalStateException
*/
fun AnkoLogger.throwError(message: Any, thr: Throwable? = null) {
error(message, thr) // AnkoLogger
kotlin.error(message) // throws IllegalStateException
}
The problem is that, even if you declare this function to be inline, Kotlin treats it differently than a direct call to kotlin.error
, where the compiler will infer that the function is never returning, and won’t give you grief about “A return expression required in a function with a body block”. kotlin.error
itself works around this by having an internal annotation (@kotlin.internal.InlineOnly
) which we’re not allowed to use in our own code.
Any thoughts on this? What’s the right way to have a throwError
directive that merges the error reporting and the throwing with the inference that control isn’t coming back to the caller?