https://kotlinlang.org logo
n

Niklas Gürtler

02/15/2021, 1:57 PM
Can I get rid of that ugly line with the
@Suppress
in https://pl.kotl.in/xmmqu1Wqf ? It is unreachable but without it, I get a type mismatch error.
r

Roukanken

02/15/2021, 2:12 PM
this looks a bit better I guess? Also way more readable not sure if there is some better way
Copy code
fun <T> retry (run: () -> T) : T {
    lateinit var lastError: Throwable

    repeat(5) {
        try {
            return run()
        } catch (currentError: Throwable) {
            lastError = currentError
        }
    }

    throw lastError
}

retry {
    println("Hello, world!!!")
}
compiler doesn't scream at me for the lateinit, so I guess it can figure out that it will be set 😄
🙌 1
n

Niklas Gürtler

02/15/2021, 2:15 PM
great idea, it works, thanks!!
4 Views