uli
10/14/2021, 8:09 PMsuspend
-edness is not inferred on functional receivers at call site.suspend fun exceptionTest() {
suspend { // <- suspend should not be needed
doSomeThing()
}.logAndDropExceptions("Something failed")
}
protected suspend fun <T> (suspend () -> T).logAndDropExceptions(message: String) {
try {
this()
} catch (e: Exception) {
log("$message: $e")
}
}
I would expect receivers to behave like parameters. This includes:
1. type inference should know that the receiver is suspend without the explicit ‘fake’ suspend
keyword
2. If I make logAndDropExceptions
inline, suspended-ness of the receiver should follow from the suspending environment of the call site and should not need any declaration at all
3. And sure I’d expect the no-overhead-benefits that we learned to expect from inline functionslogAndDropExceptions("Something failed")
before the block does not feel right
suspend fun exceptionTest() {
logAndDropExceptions("Something failed") {
doSomeThing()
}
}
protected inline fun <T> logAndDropExceptions(message: String, block: () -> T) {
try {
block()
} catch (e: Exception) {
log("$message: $e")
}
}
ephemient
10/14/2021, 9:42 PMfun ((Int) -> Unit).f() = Unit
{ println(it) }.f()
fails to compileuli
10/14/2021, 9:44 PMelizarov
10/21/2021, 4:11 PM{ … }.doSomething
design yours APIs around doSomething { … }
, pleaseuli
11/01/2021, 3:24 PM{
// some code
}.logAndDropExceptions("Something failed")
instead of
logAndDropExceptions("Something failed") {
// some code
}
elizarov
11/04/2021, 9:28 PMuli
11/04/2021, 9:29 PMelizarov
12/01/2021, 10:42 AM{ … }.doSomething()
since this kind of code is not expected to appear in idiomatic Kotlin. Idiomatic Kotlin is all about readability. The code shall read like prose. In order for human to read and understand this code it shall start with some introduction, setting context.