How do I suppress "Unchecked cast" warnings?

I've tried the regular @SuppressWarnings("unchecked") on top of the method that makes these unchecked casts (which are 100% safe but the Kotlin compiler can't see that) and yet, I still see the warning.

Does kotlinc ignore @SuppressWarnings? Is there a Kotlin equivalent?

Hi Cedric,

I think @suppress(“UNCHECKED_CAST”) is what you’re after.

It can be applied at various levels (statement, method, etc).

Cheers,
Andrew

Indeed, thanks Andrew.

I also found Kotlin’s version of this annotation: @suppress (should probably be capitalized).

Hi Cedric,

Regarding captialisation of annotations, they’re lowercase so they can be used like keywords (the @ is ususally optional). e.g. the “data” annotation.

data class Foo(val bar: String)

It gets a bit messy using Java annotations as they use capitals, but I tend to rename the common ones on import.

import org.junit.Test as test test fun shouldDoFoo() { }

Personally, I like the convention - it's just a little awkward when using annotation heavy Java frameworks.

There was some talk by the Kotlin devs of relaxing case constraints for annotations but I’m not sure if it’s still on the roadmap.

Cheers,
Andrew

Yes, I am aware of the discussions.

I think it’s unfortunate that Kotlin can’t quite make up its mind here, although it’s understandable for a pre 1.0 language (we already switched from [Test] to @Test so I assume further changes are not ruled out).

I think it would be unfortunate to ship 1.0 with the option to either use @ or not and the option to either capitalize annotations or not. This will lead to messy code base.

I’m probably a bit biased since I was part of the experts group that created the Java annotation spec but for better or for worse, “@” and capitalization are so widespred in Java that Kotlin should probably adopt it as well.