简体   繁体   中英

How to understand Kotlin documentation syntax in Android Studio

Example

I'm hovering over a ClassName::javaClass instance in my kotlin/android project - and see a preview of the documentation for javaClass :

public val <T : Any> T.javaClass: Class<T>

Question

Apologies if this is a basic question but how can the above be translated? And is there any resource for me to read that will give me a rundown of that type of documentation syntax?

public val <T : Any> T.javaClass: Class<T>

This is actually extension property. Works basically the same as extension function (in fact compiler will generate extension getter function for any type inheriting from Any ).

Simpler example, without generics:

val String.withPrefix: String
    get() = "_$this"

fun test() {
    println("Hello".withPrefix) // prints "_Hello"
}

One more example - now for generic type:

val <T: Number> T.hexValue: String
    get() = convertToHex(this)

fun main(args: Array<String>) {
    val number: Double = 10.0
    println("Hex value of $number is ${number.hexValue}")
}

Construction you are asking is quite strange, because connects both generic and extension function. But, as you can see in fact there is just very simple construction - just looks strange.

See more about extensions: https://kotlinlang.org/docs/reference/extensions.html#extension-properties

See more about generics: https://kotlinlang.org/docs/reference/generics.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM