|The Programmers Guide To Kotlin - Generics|
|Written by Mike James|
|Monday, 04 June 2018|
Page 1 of 2
Generics are an essential part of any modern language. Kotlin has generics and at first they look like any other languages generics but, as with most Kotlin features, they have some interesting differences.
Programmer's Guide To Kotlin
Now Available as a Print Book
You can buy it from: Amazon
Some Chapters Already Available On The Web
We start out programming not really worrying too much about type and then it becomes central to everything we do. Everything has a type and when you write code it works with a specific set of types. However, some algorithms are the same no matter what type they work with.
For example, a sorting algorithm doesn't really care what it is sorting as long as it has a way to decide if one element is bigger than another.
Yet in a strongly typed language you have to write a sort routine for each type you want to sort. Of course this isn't what happens. If you want to do something in a type independent way then you can give up strong typing and work with Any and down casting.
Unfortunately an array of Any objects isn't very useful as Any doesn't bring with it many methods and these are all you can use. To make a typed approach to creating generics work you have to have a base class that has the methods you want to use to manipulate all of the derived classes, and class hierarchies generally aren't designed with this in mind.
For example Number is the top level class for any numeric type in Kotlin, but it doesn't have a comparison operator that is defined in Number. This means you can' t write a sorting function using this approach.
As Number has an equality operator, you can, however, write a find function:
This works because the array type is Array<out Number> and Number is the base class for all numerics. Don't worry about the use of out in the array declaration, it is explained later.
The problem is that as a Kotlin array is defined as a generic we can't actually avoid generics in this implementation.
With this function definition we can now call find using an array of elements of any numeric type.
However if you try:
You will find that it doesn't work for Array<Char>. To make it work you would have to change Number to Any as Char isn’t a numeric even if it has a comparison operator.
This is a completely general approach to implementing algorithms that work with a range of objects types. All you have to do is write a function that accepts the base class for all of the objects and work with it. If you want to access any of the methods beyond the base class you will need a down cast.
This approach is workable but not particularly flexible. Generics is a purpose built solution for writing general algorithms that work on a range of types in a type-safe way.
However, it is worth pointing out that Generics don't offer that much above working with variables that reference Any. They often impose restrictions which have to be overcome to implement what you want. They are not the perfect solution to type-free programming.
The basic idea of generics is very simple – allow type parameters in class, interface and function definitions. To make use of such generic declarations you have to supply real type for the type parameters at the time they are used.
A type parameter is defined by being enclosed in <> used as brackets and can be used anywhere a type specification can be used.
So for example the function that we used earlier to find an element in an array can be re-written:
The <T> at the start declares the type parameter. You don't have to use T and you can have as many type parameters as you like. Within the function definition, the type parameter is just used as T without angle brackets. Notice that Array is now clearly a generic type as it too has a type parameter that we have to specify using the same angle bracket form.
Following this we can call the generic function:
which will work assuming that b is an array of Ints.
Class and Interface generic declarations work in the same way – add a type parameter in angle brackets at the start of the declaration and use the parameter as if was a type in the body of the declaration.
declares a class with a single type parameter and a method that accepts a single parameter of that type. You can include a base class and interfaces within the class definition.
That is, apart from the use of the type parameter, the class declaration is standard. The same is true for a generic interface.
Now we come to the big restriction inherent in using a generic. As the type parameter is unknown at the time of declaring the class, interface or function, you can't use any of its methods other then the ones supported by Any.
So for example if you try to write:
the result is an error message saying that the system can't work out what a+b is.
This isn't unreasonable as without knowing what T is, how can the system determine at compile time how to implement the + operator?
A second problem is that at runtime the type of any parameter passed to the function is lost. That is, if you pass an Int as a and b, you can’t discover this at runtime – the types of the parameters are erased. This is obviously enough called type erasure and it is how Java implements generics.
As a consequence of not being able to do anything to a type that you don't know also notice that you cannot define a standard generic property within a class. The reason is that it would have to be initialized and what do you initialize an unknown type to?
will cause the compiler to insist that you initialize the property or declare it virtual and you cannot create an instance of T as you don't at this stage know what T is. If you set myProp to a nullable type then you can set it to Any:
but this forces all instances to have a property of type Any which isn't really what you want.
What you can do is create a property using the primary constructor as this forces an initialization when the constructor is called, and Kotlin is satisfied that the property is initialized:
After this you can create an instance with a property of the correct type:
prints 10 and Int.
So, if you want a generic property, use the primary constructor.
|Last Updated ( Monday, 04 June 2018 )|