The second approach is more Kotlin-friendly. A Smart Conversion? private fun getDefaultSize(anyObject: Any): Int Result of the first approach: an error message is triggered I’m getting there! Let’s write the same method right away, but in Kotlin. The out Keyword – In Kotlin, we can use the out keyword on the generic type which means we can assign this reference to any of its supertypes.OK, I had figured that out all by myself! But what does this have to do with Kotlin and this chapter? Declaration-site variance(using in and out).Any is the supertype of all other datatypes. Invariance is the property by which a standard generic function/class already defined for a particular data type, cannot accept or return another datatype. ![]() This can be managed by the out and in keywords. By extension, generic types are invariant in Kotlin. Unlike Java, Kotlin makes arrays invariant by default.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |