https://kotlinlang.org logo
#codereview
Title
# codereview
r

rook

01/15/2021, 4:39 PM
Hello, I made this a few weeks ago and I just wanted to get some feedback on the implementation and maybe on the usefulness/syntax/design of its use?
I’m sure it can be extrapolated, but the usage looks something like
Copy code
val myBundleString: String by BundleDelegate.of(MY_BUNDLE_STRING_KEY)
j

Joel

01/15/2021, 10:06 PM
I think you can use:
Copy code
return extras.get(key)?.let { clazz.cast(it) } ?: throw Exception("Key does not exist in bundle.")
This will throw a
ClassCastException
if the value is not
clazz
, and you don't have an unchecked cast.
💯 1
r

rook

01/15/2021, 10:31 PM
Thanks! That’s a good point! I didn’t think to fall back on the built in
ClassCastException
j

Joel

01/15/2021, 10:32 PM
I feel like you could do this entirely with lazy, assuming you are inside of an
Activity
Copy code
inline fun <reified T> Activity.fromBundle(key: String) = lazy {
    intent.extras[key] as? T ?: throw Exception("Key does not exist in bundle") 
}
r

rook

01/16/2021, 4:38 AM
Interesting. I’ll have to toy with it. That’s a much cleaner approach, but I feel like some of the debugging fidelity is lost in the brevity.
2 Views