waqas
06/11/2018, 2:26 PMbenleggiero
06/11/2018, 2:40 PM@autoclosure
, another feature I'd like in Kotlin. It allows you to define a closure argument that takes nothing and returns a type. Then, the caller provides anything that resolves to that type and the compiler converts it into a closure.
From the caller's perspective, it's just the value, but in reality it's a lazily-evaluated closure.
For example:
// Swift
func log(trace message: @autoclosure () -> String) {
if logLevel <= .trace {
logImmediately(level: .trace, message: message())
}
}
log(trace: "My \(interpolated) string")