Meet Kotlin 1.1: JavaScript, Coroutines, and More

Original author: Andrey Breslav
  • Transfer

We are pleased to introduce Kotlin 1.1, the new version of the Kotlin programming language.


Kotlin 1.1

Our goal is to create an expressive statically typed language in which all components of a modern application can be effectively written. Today's release takes two important steps in this direction.



Firstly, JavaScript support has ceased to be experimental. All the features of the language are available, a significant part of the standard library and, of course, the ability to interact with JavaScript code. Now the client part of the application can also be migrated to Kotlin, and there is no need to abandon the usual libraries and frameworks such as React.


Secondly, there is support for corutin, which is a lightweight analogue of threads, which allows you to scale applications well, supporting a large load on one machine. In addition, coroutines greatly simplify the writing of asynchronous code, which is important for the “responsiveness” of the UI on all platforms.


We will tell you more about these changes below. Other innovations include type aliases , bound callable references , destructuring in lambdas . A full list of interesting changes can be found on our What's new page (pay attention to running examples!).


Coroutines (Coroutines)


Coroutines in Kotlin make writing asynchronous code as easy as regular synchronous code.


Asynchronous code is getting bigger, and writing it is often quite difficult. To facilitate this work, we have added Kotlin language-level coroutine support with just one primitive - suspending functions. The execution of such functions can be suspended without blocking any thread and resumed later. Moreover, the control over the logic of suspension and resumption of execution belongs entirely to the author of the library, which allows the use of coroutines as a lightweight replacement of threads. Now, instead of expensive blocking the flow, you can use almost free suspension of coroutine.


Very little has been added to the syntax of the language (one modifier), but the libraries are given a huge scope for activity. For example, in the kotlinx.coroutines project , we already implemented support for Rx, CompletableFuture, NIO, JavaFx, and Swing. Such libraries can (and are planned) written for Android and JavaScript. Even constructs that are built-in in other programming languages ​​can be implemented in Kotlin in a library: for example, generators / yield from Python, channels / select from Go or async / await from C #:


// runs the code in the background thread pool
fun asyncOverlay() = async(CommonPool) {
    // start two async operations
    val original = asyncLoadImage("original")
    val overlay = asyncLoadImage("overlay")
    // and then apply overlay to both results
    applyOverlay(original.await(), overlay.await())
}
// launches new coroutine in UI context
launch(UI) {
    // wait for async overlay to complete
    val image = asyncOverlay().await()
    // and then show it in UI
    showImage(image)
}

Documentation Details


Important notice . Despite all the advantages that coroutines give, their design is still relatively young. Therefore, an intensive verification of it in practice is required in order to make sure that we have done everything correctly. Therefore, in 1.1 coroutines come out under the “experimental” flag . We assume that language constructs are unlikely to change, however, in Kotlin 1.2, changes to the coroutine-related API are possible.


JavaScript support


As we said above, all the features of Kotlin are now equally available for both JVM / Android and JavaScript (except for reflection, but we are working on it). This allows you to develop a web application entirely on Kotlin, which we successfully do in some products inside JetBrains. We plan to publish the relevant documentation and instructions in the near future.


Kotlin for JavaScript supports dynamic types for interacting with JavaScript code. For typed access to popular libraries, you can use ts2kt converter with headers from DefinitelyTyped .


Documentation Details


Tools


We are trying to release new functionality not related to the syntax of the language in intermediate releases. Here is the most striking thing that has appeared since Kotlin 1.0:


  • Plugins for all major IDEs: IntelliJ IDEA, Android Studio, Eclipse and NetBeans
  • Incremental compilation in IntelliJ IDEA and Gradle
  • Compiler plugins for better integration with Spring, JPA and Mockito (extensible classes by default and automatic generation of empty constructors)
  • kapt - annotation tool
  • Code checks specific to Android projects
  • And of course, many, many new diagnostics, checks, quick fixes, refactoring, and improvements to the auto-completion have been added to the IDE

But we do not stop and are already preparing improvements in the toolkit for versions 1.1.x.


Kotlin's first year: community and distribution


In short, we are growing! Over 160 thousand programmers have used Kotlin over the past year. The number of lines on Kotlin in open source projects on Github has grown from 2.4 to 10 million. At the time of release 1.0, there were 1'400 people in the Kotlin slack channel - now 5'900. Every week , new mitaps and user groups appear around the world , organized by active members of the community. More and more new books and online courses are being published.


image


Kotlin is about equally popular among server-side and Android developers. Spring Framework 5.0 introduces support for Kotlin , as well as vert.x 3.4 . Gradle and TeamCity use Kotlin-based DSL to program build scripts. A list of all the main Kotlin projects and libraries can be found at kotlin.link .


A large number of world-famous companies began to use Kotlin in product code: Amazon Web Services , Pinterest , Coursera , Netflix , Uber , Square , Trello , Basecamp and many, many others. Corda - a distributed workflow system developed by a consortium of well-known banks (such as Goldman Sachs, Wells Fargo, JP Morgan, Deutsche Bank, UBS, HSBC, BNP Paribas, Société Générale), was developed from the very beginning on Kotlin . Kotlin is also used in Russian companies - for example, Avito, Rocketbank and Aviasales.


We are endlessly grateful to all our users, contributors and active supporters in all corners of the world. Your support is very important to us!


Build Your Kotlin 1.1 Event


The release is a good reason to meet other Kotlin users. We have prepared a set of materials that will help you organize such a meeting in your city: on March 23 we will broadcast a live meeting with representatives of the Kotlin team, plus you can get souvenirs and a set for Future Features Survey. More information and registration form here .


What's next


In order to make Kotlin a truly universal full-stack language, we continue to work on language and tool support for compiling common code for several platforms at once. This will allow writing modules whose code is used both on the client side and on the server side. We also continue to improve tools and support for third-party JavaScript libraries. Incremental compilation for JavaScript is on the way, we will release it in one of the versions 1.1.x.


Support for Java 9 will appear by the release of the official version of this platform (this summer).


We expect to see a lot of feedback on coroutines and will work to improve design, performance and libraries as they become available.


But, by and large, our next release will primarily focus on the quality of the tools for existing language features, improving performance, expanding the infrastructure and fixing bugs. Kotlin is gradually becoming a key technology for a large number of projects, and we respond accordingly to our growth.


PS Distribution of Kotlin on all major platforms is a strategic direction for us. We can already run on servers, desktops, Android devices and browsers. In the foreseeable future, we plan to cover the “world without virtual machines” and support such platforms as, for example, iOS and embedded systems. Now the excellent Kotlin Native team is working on it at JetBrains, and we hope to show something interesting in a relatively short time.


How to try


As always, you can try Kotlin online directly in your browser: try.kotlinlang.org .
Maven / Gradle : Use 1.1.0 as the version number of the compiler and standard library.
IntelliJ IDEA : Kotlin 1.1 support is already built-in in 2017.1, in earlier versions you just need to update the plug-in to version 1.1.
Android Studio : Install or update the plugin using the Plugin Manager .
Eclipse : A new plugin is available in the Marketplace .
The command-line compiler can be taken from the release page on Github


Compatibility . In Kotlin 1.1, the language and the standard library are fully backward compatible (modulo bugs): i.e. if something was built and started under 1.0, it will continue to work in 1.1. To facilitate gradual migration for large teams, we have provided a special compiler key that disables all new language features in 1.1. In this document, we have described in detail all the subtleties of migration.


Also popular now: