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 -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -

How to understand 2 main() functions after using uftrace to profile the C++ program? -