natario1
09/23/2022, 9:22 AMval list: List<Pair<String, Any>> = …
val doubles = list.filterIsInstance<Pair<String, Double>>()
filterIsInstance can’t check inner generic types, but this code is clean for both IDE and compiler.Sam
09/23/2022, 9:31 AMnatario1
09/23/2022, 9:36 AMnatario1
09/23/2022, 9:39 AMSam
09/23/2022, 10:12 AMval s = "Hello, World"
val i1: Int = s as Int // warning: "This cast can never succeed"
val i2: Int = s.unsafeCast() // does the same thing, but with no warning 🤦
fun <A, B> A.unsafeCast(): B =
listOf(Pair(this, null)).filterIsInstance<Pair<B, Nothing?>>().single().first
ephemient
09/23/2022, 12:08 PMnatario1
09/23/2022, 12:33 PMinstance is ReifiedTypeWithGenerics
I think… Just like I get a warning if I do listOfInts is List<String>ephemient
09/23/2022, 12:35 PMinstance is ReifiedTypeWithGenerics
in your case though, it sees a .inlineFunctionCall<ReifiedTypeWithGenerics>()
, which may (or may not) be safenatario1
09/23/2022, 12:37 PMis
check and since the function is inlined, it’s probably possible to report the warning/error when I compile my code with itephemient
09/23/2022, 12:38 PMis
check in my safe variant as well, that's not something you can warn onephemient
09/23/2022, 12:39 PM.filterIsInstance<>()
, I just don't think it can be a general rule but can only be on specific known functions