January 2018
Intermediate to advanced
434 pages
14h 1m
English
Now, let's follow these steps to understand how Generics works in Kotlin, with the help of some examples:
fun main(args: Array<String>) { val intgen: GenCl<Int> = GenCl<Int>(10) println(intgen.a) // We are letting Kotlin compiler infer type val strgen = GenCl("A string") println(strgen.a)}class GenCl<T>(t: T) { var a = t}
The output of this program is this:
10A string
fun main(args: Array<String>) { val intgen: GenCl<Int> = GenCl<Int>(10) println(intgen.a) val flgen = GenCl(1.0) println(flgen.a)}// Restricting T to only be of type Numberclass GenCl<T: Number>(t: ...Read now
Unlock full access