In Scala I can write something like this:
val something = {
val temp1 = ...
val temp2 = ...
temp1 + temp2
}
As far as I know the best way to do the same in Kotlin is:
val something = {
val temp1 = ...
val temp2 = ...
temp1 + temp2
}()
Actually it's a lambda with type Unit -> Int which is called immediately. I wonder could this code be improved somehow? Maybe there is a built in function which allows me to write val something = block { ... } or something like this?
You can use function run
, like:
val something = run {
val temp1 = ...
val temp2 = ...
temp1 + temp2
}
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.