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

Popular posts from this blog

'hasOwnProperty' in javascript -

javascript - Knockout pushing observable and computed data to an observable array -

c# - Update a combobox from a presenter (MVP) -