Hello, I made this a few weeks ago and I just want...
# codereview
r
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
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
Thanks! That’s a good point! I didn’t think to fall back on the built in
ClassCastException
j
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
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.