Generics let us create classes that work with any type. Learn how to set up and use them in Kotlin. Also see how bounded types make generics usable.
- [Instructor] Generics are a way to create a class or method that can work with any type. Generics in Kotlin are similar to those in Java except they are somewhat easier to work with. Let's imagine that we would like to create a function, max. Max takes two parameters of the same type, compares them, and returns the greater of the two. Since we would like to make max use any type we'll make max a generic function. Let's use our hypothetical generic function with a series of calls and a variety of types.
So we'll go ahead and scroll back up here. We will create a val maxInt of type Int and set it equal to the results of max(42, 99). Now remember, max doesn't exist yet so we're going to get some warnings. Next we'll say maxLong: Long = max(123456789L, 999999999999L).
Now we'll do a byte, maxByte it is of type Byte and we're going to say max. Now in order to use bytes, we'll convert them from integers. So we will say -128 and we'll convert it to bytes. Likewise we will say (127).toByte.
And finally we will create a string so maxString, type string, is max("Beta", "Alpha"). Next, we'll print them all out. So println("The max Int = $maxInt").
"The max Long = $maxLong". For a byte "The max Byte = $maxByte"). And finally we'll get to the string. "The max String = $maxString".
Now again this code won't compile until we've actually created our max function and to create it we'll go over here to com.tekadept.demo, do a right click, do a New, Kotlin FileClass, we're going to name this file max. Inside of it we'll create our package which is going to be the same package we've been using com.tekadept.demo. And in here we'll just create a fun.
We're going to say <T> max(param1: T, param2: T). This whole thing returns type T and it's a function so we've got an open and closed curly brace. Let me go fix that two. That should be a T. Alright so what we have here is sort of the outline for the definition of this function.
The T in the angle brackets is the type parameter. Think of it as a placeholder. It will be replaced at compile time with a specific type. We are saying create a function max which takes two parameters of type T and returns type T. As we think about our problem, initially the solution seems obvious. We can easily compare any two numeric types. But what about strings? How do we compare those? In Kotlin, we can compare two variables of the same type by using the compareTo method. Let's use compareTo to help us write our function.
So now we're going to say that val results = param1.compareTo(param2) and then finally what we'll say is return if(results > 0) param1 else param2. Now we can see that the compareTo is in red and the problem here is that not every type has a compareTo method.
Only the ones derived from Comparable. We somehow need to restrict our generic function so it only accepts types of Comparable. We can do that by placing an upper bound on our function's type parameter. So what we do is we come in here to the first T that's in the angle brackets. We're going to put a colon and then we're going to put the word Comparable because all the types that we want are going to be derived from Comparable and we're going to say <T>.
So if we look at it now we see no red lines. We placed an upper bound by modifying the type parameter. We add a colon Comparable to it. This restricts our function. Now let's run our code. So we'll go back to the main function and we will say execute and voila. We have max Int is 99, the max Long, the max Byte, the max String. Generics let us write code which can work with a variety of different types.
- Kotlin as a better Java
- Setting up a Kotlin programming environment
- Val vs. var
- Understanding basic Kotlin programming concepts
- Object-oriented programming
- Using Java from Kotlin
- Using Kotlin from Java
- Annotations, reflection, and DSL construction
- Functional programming in Kotlin