https://kotlinlang.org logo
#announcements
Title
# announcements
j

jlleitschuh

12/22/2016, 2:59 AM
This is pretty cool:
Copy code
/**
 * Suppresses simple exceptions that may be thrown by this code executing.
 */
infix fun <R, T : () -> R> T.doNotFail(doNotThrow: Boolean): R? {
    try {
        return this()
    } catch (ex: Exception) {
        when (ex) {
            is RuntimeException,
            is SQLException -> {
                if (doNotThrow) {
                    logger.debug("Failure was suppressed", ex)
                    return null
                } else {
                    throw ex
                }
            }
            else -> throw ex
        }
    }
}
That allows this code to be valid:
Copy code
{ statement.executeUpdate("DROP DATABASE ${db.dbName}") } doNotFail true
{ statement.executeUpdate("DROP ROLE ${db.username}") } doNotFail true
Also, that this is valid code is strange:
Copy code
{}()
Reminds me of javascript's self executing functions.