⬆️ ⬇️

Kotlin M4 released

Kotlin M4 came out, another milestone of our programming language . Now Kotlin is compatible with JDK7, has learned to better deduce types, become faster. The debugger, IDE integration with JUnit, has been greatly advanced, a new build mechanism is supported, which will eventually grow into an incremental compiler ... In this post I will briefly tell you about the most interesting, you can read more about M4 here (in English).





Kannotator





As you probably remember, the Kotlin type system is somewhat richer than the one in Java. In particular, Kotlin controls null references at the type system level. It works great, until there is a need to interact with Java code in which it is not written anywhere that can be null and what is not, and the Kotlin compiler has to assume the worst: that everything can be null. This approach is safe, but not very convenient, so in the previous release, Kotlin M3, we supported the external annotation mechanism, which allows the user to explain to the environment that this or that method never returns null (in fact, you can tell the environment much more but more about that another time).

')

By the way, these annotations are useful not only for Kotlin, there are excellent inspections in IntelliJ IDEA that help to avoid NPE in Java , we use them all the time at JetBrains, and are very satisfied.



However, it is difficult to annotate large libraries manually, and it is not always clear from their code which annotations need to be specified. Therefore, we developed a special tool (written, of course, on Kotlin) - KAnnotator, which reads the byte-code of the libraries and automatically outputs the necessary annotations. It works like this: a set of jar-files arrive at the input, KAnnotator analyzes them, and outputs xml-files containing annotations in approximately the following form:

image

These annotations can be connected to your project, and both the IDE and the Kotlin compiler will see them.



KAnnotator is still very young , he will develop and grow wiser, but today we already have an automatically annotated version of the JDK , which is included in M4.



Copy for Data Classes





Data classes appeared in M3, and many users asked to support the ability to copy objects of these classes, replacing the values ​​of some fields (the objects themselves are often immutable). In M4, this feature is supported:



data class Person(val firstName: String, val lastName: String)





fun Person.asMarried(newLastName: String)

= this.copy(lastName = newLastName)





The copy () function is automatically generated for all data classes, each of its parameters has a default value, so when copying, it is enough to specify only those properties whose values ​​change.



Java variation





Generic classes in Kotlin allow you to specify the variation of typical parameters, and the collection library has read-only interfaces that indicate the variation is correct, so the List , List. Kotlin. Java?



M4, Kotlin -, Java. , :



fun join(l: List, separator: String): String = ...



:



String join(List<? extends Object>, String separator)





, Java List, .



...



, , , Kotlin, ... .



IntelliJ IDEA 12, , ( Community Edition ). .



!
List , List. Kotlin. Java?



M4, Kotlin -, Java. , :



fun join(l: List, separator: String): String = ...



:



String join(List<? extends Object>, String separator)





, Java List, .



...



, , , Kotlin, ... .



IntelliJ IDEA 12, , ( Community Edition ). .



!

List , List. Kotlin. Java?



M4, Kotlin -, Java. , :



fun join(l: List, separator: String): String = ...



:



String join(List<? extends Object>, String separator)





, Java List, .



...



, , , Kotlin, ... .



IntelliJ IDEA 12, , ( Community Edition ). .



!

List , List. Kotlin. Java?



M4, Kotlin -, Java. , :



fun join(l: List, separator: String): String = ...



:



String join(List<? extends Object>, String separator)





, Java List, .



...



, , , Kotlin, ... .



IntelliJ IDEA 12, , ( Community Edition ). .



!

Source: https://habr.com/ru/post/161327/



All Articles