I'm looking at the code that I wrote before. There...
# getting-started
k
I'm looking at the code that I wrote before. There is cache:
val cache = MutableMap<String, Any?>
where value could be String, Int, Map, Array, etc. Then i retrieve values from it through the chain of functions, where caller is responsible to specify correct type.
cache[key] as R?
Question: what problems I may get with that approach? All of this code is internal to the module, and run on jvm/ios.
k
If the user messes up and they don't specify the right type you'll get a classcastexception.
m
It would be safer to use:
cache[key] as? R?
. That way wrong types would be ‘ignored’. The result would be null then.
k
Except then there's no difference between "not in the cache" and "programmer error"
☝️ 1
👍🏻 3
k
thx, classcastexception is fine in this case
k
Although that exception will only occur when the generic code ends, so if for example people just put it in some other generic code the exception might not happen immediately.