Kotlin Unwrapping Function Compiler Error -
anyone know why following code doesn't work?
private fun wraplogifneeded(buildmessageoncurrentthread: boolean, log: () -> string): () -> string return if(buildmessageoncurrentthread) { val message = log() // type mismatch: required () -> string found: unit { message } } else { log } }
but does:
private fun wraplogifneeded(buildmessageoncurrentthread: boolean, log: () -> string): () -> string return if(buildmessageoncurrentthread) { val message = lazy { log() }.value { message } } else { log } }
that's because of syntax ambiguity:
val message = log() { message }
this code gets parsed if val message = log() { message }
, is, log
called lambda { message }
argument. , statement val message = ...
has type unit
, hence error message.
to resolve it, can add semicolon:
val message = log(); { message }
Comments
Post a Comment