ilya.gorbunov
01/17/2017, 2:44 AMopas90
01/17/2017, 2:44 AMilya.gorbunov
01/17/2017, 2:48 AMcompute
function instead:
cache.compute(agentId, { id, value -> value ?: getUserFromDB(agentId) })
opas90
01/17/2017, 2:51 AMilya.gorbunov
01/17/2017, 2:52 AMopas90
01/17/2017, 2:58 AMilya.gorbunov
01/17/2017, 3:15 AMcache
might be blocked while getUserFromDB is executing?cedric
01/17/2017, 3:17 AMcedric
01/17/2017, 3:17 AMcache
is @opas90's own cache implementation?ilya.gorbunov
01/17/2017, 3:22 AMopas90
01/17/2017, 3:26 AMopas90
01/17/2017, 3:26 AMcedric
01/17/2017, 3:26 AMcomputeIfAbsent
is the right methodopas90
01/17/2017, 3:32 AMcedric
01/17/2017, 3:32 AMString
, since the look up can fail, correct?opas90
01/17/2017, 3:33 AMilya.gorbunov
01/17/2017, 3:33 AMConcurrentHashMap<Long, String>
and cast it to ConcurrentHashMap<Long, String?>
before calling computeIfAbsent.opas90
01/17/2017, 3:34 AMopas90
01/17/2017, 3:35 AMopas90
01/17/2017, 3:36 AMilya.gorbunov
01/17/2017, 3:37 AMunchecked cast warningYes, but it's the case when it's safe to ignore or suppress it, as you know that computeIfAbsent would never put null into map.
miha-x64
01/17/2017, 8:21 AMokkero
01/17/2017, 8:22 AMinaction
01/17/2017, 9:24 AMclass People(n: String) {
val name = n
}
class Test {
var me: People? = null
fun initial() {
me = People("Inaction")
}
fun hi(name: String) {
println(name)
}
fun doSomething() {
initial()
me?.let {
hi(me.name)
}
}
}
fun main(args: Array<String>) {
Test().doSomething()
}
Check the codes in doSomething
function, and I got the error:
Smart cast to 'People' is impossible, because 'me' is a mutable property that could have been changed by this time
So what should I do to solve this error?uhe
01/17/2017, 9:26 AMhi(it.name)
inaction
01/17/2017, 9:27 AMinaction
01/17/2017, 9:27 AMkotlin
always put an it
in the context, right? @uheuhe
01/17/2017, 9:28 AMinaction
01/17/2017, 9:28 AMmiha-x64
01/17/2017, 9:29 AMit
is default name of lambda’s single argument.