Having the following code:
fun doSomething(): List<String> {
val test: List<*> = arrayListOf("test1", "test2")
return test as List<String>
}
Is there some way to suppress the unchecked cast warning that comes up in the last line? I tried to use the standard Java way @SuppressWarnings("unchecked")
at the method level, but it didn't work.
@SuppressWarnings("unchecked") is used when Java generics just don't let you do what you want to, and thus, you need to explicitly specify to the compiler that whatever you are doing is legal and can be executed at the time of execution.
1 Answer. Show activity on this post. Adding @Suppress("UNCHECKED_CAST") (also possible through IDEA's Alt + Enter menu) to any of statement, function, class and file should help.
Unchecked cast means that you are (implicitly or explicitly) casting from a generic type to a nonqualified type or the other way around.
Adding @Suppress("UNCHECKED_CAST")
(also possible through IDEA's Alt+Enter menu) to any of statement, function, class and file should help.
Before:
After:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With