简体   繁体   中英

Kotlin unit not returning string

The second parameter of this code must be a string and both email and currentGroup are not null or empty. when I use the normal if else statement, it works. But when i use kotlin lambdas i get an error. I also tried to add .toString() at the end of the second parameter but had no luck.

query.whereEqualTo("currentGroup",{
    if (isAdmin) email else currentGroup
})

I also tried to log the second parameter and got this output: Function0<java.lang.String>

From your last comment it is clear that your second parameter of your function is a String , so either use a String directly or call your lambda, eg:

query.whereEqualTo("currentGroup",{
  if (isAdmin) email else currentGroup
}()) // see the ()? Alternatively/Preferably you may use run {} here, as it is already an inline function (see below)

But it is definitely easier to just use if - else here... you do not gain something from introducing lambdas which are just immediately executed:

query.whereEqualTo("currentGroup",
  if (isAdmin) email else currentGroup
)

Note also that many functions use inline just to overcome the runtime penalties associated with lambdas... so you may want to read about that too before you start liking lambdas and use it everywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM