Generic types in Kotlin

Just as in Java 1.5 onwards, Kotlin also has support for generic types to ensure type and value safety at compile time and run-time to reduce boilerplate code and errors. Generics can be used in the following way:

class Range<F, T>(var from: F, var to: T) {

override fun toString() : String {
return "From $from to $to";
}
}

Generics can also be used with different data types, such as in the following code:

var intRange : Range<Int, Int> = Range<Int, Int>(1, 10);
var doubleRange : Range<Double, Double> = Range<Double, Double>(1.0, 10.0);

println(intRange);
println(doubleRange);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.67.40