|Kotlin 1.5 - Mature But Still Growing|
|Written by Mike James|
|Tuesday, 18 May 2021|
A 1.5 release is always interesting and Kotlin continues its development into a "do-it-all" language with its latest update.
The first thing to say is that Kotlin is a very mature language and there isn't going to be much scope for radical changes. This said, it has to keep an eye on developments in Java in order to remain compatible. Hence it has now graduated from experimental to stable its support for JVM records. Kotlin has had its own data class for ages and now Java has caught up. The only real difference between the two is that JVM records don't have a copy method, but data classes do. All you have to do to ensure compatibility with JVM records is use a @JvmRecord annotation.
The other improvements are equally minor. Subclasses of sealed classes can now be spread through a compilation unit whereas previously they all had to be in the same file.
An interesting introduction is the idea of an inline class - although the terminology is a bit outdated. There is nothing much inline about an inline class and they would better be called "value classes". Primitive data types like Int are value classes. Primitive value are often used "raw", i.e. without any pretense that they are objects, but to maintain the idea that they are objects they can be "boxed" so as to look like an object. One key difference is that such value objects generally don't maintain their identity unless they are boxed. That is, one 3 is the same as any other 3 - unless the value is boxed when each boxed value object is different, even if they happen to represent the value 3.
This is the sort of subtle stuff that can drive you mad and now you can go mad more effectively with the introduction of custom value classes. At the moment value classes are limited to a single field so you can't create a complex number value class. For example:
inline class Color(val rgb: Int)
defines a class that is essentially the same as an Int and indeed is substituted for by an Int where ever possible i.e. no wrapper, no boxing. This sort of thing is of limited use but it does allow you to create custom scalar and String types.
The biggest improvements aren't really in the language, but in its implementation and support. The new IR (Intermediate Representation) backend provides a unified compiler for the three main branches of Kotlin implementation - JVM, JS and Native. This means that any fixes or improvements are applied to all three implementations making using any one of them a better proposition.
Kotlin 1.5 is a good step to have made during Kotlin's tenth year of being a better Java. It is such a better Java that there isn't much room left for making it a better Kotlin. But this is no problem and it's a really great language that is both fun and efficient.
or email your comment to: email@example.com
|Last Updated ( Friday, 19 August 2022 )|