sreich
08/15/2022, 8:03 PMclass CliOption2<T>(additionalProperties: MutableMap<String, Any>) {
val value by lazy {
val t = additionalProperties[cliOption.opt]!! as? T
t ?: throw NotImplementedError("cli option could not be casted")
t
}
how can i fix the cast warning here? I thought me throwing something if it fails would work. Why is it still considered unsafe and how can i fix this?ephemient
08/15/2022, 8:05 PMas? T
doesn't actually know what T
is and always succeeds even if it's not correctRuckus
08/15/2022, 8:10 PMT
in your case is effectively Any?
, and since everything is indeed Any?
, it's always true.sreich
08/15/2022, 8:10 PMsreich
08/15/2022, 8:10 PMsreich
08/15/2022, 8:10 PMRuckus
08/15/2022, 8:11 PMreified generics
, but that only applies to functions that are inlined at compile time, and thus the generic doesn't exist at runtime.sreich
08/15/2022, 8:12 PMRuckus
08/15/2022, 8:13 PMephemient
08/15/2022, 8:13 PMephemient
08/15/2022, 8:15 PMval kclass: KClass<T>
then you could use that to check isInstance
. (you'd still have an unsafe check but at least you'd have a justifiable reason to suppress)sreich
08/15/2022, 8:16 PMephemient
08/15/2022, 8:16 PMsreich
08/15/2022, 8:16 PMephemient
08/15/2022, 8:17 PMsreich
08/15/2022, 8:17 PMval value by lazy
could be changed to a inline getValue() {}sreich
08/15/2022, 8:17 PMephemient
08/15/2022, 8:17 PMephemient
08/15/2022, 8:18 PMsreich
08/15/2022, 8:18 PM