Xi Big pu Ben! Kotlin 1.1 Beta is coming

2017-01-20
bennyhuo
Kotlin
Kotlin

WeChat ID KotlinX

Intro talks about Kotlin, the Java Virtual Machine, and the life of code


Yesterday, Kotlin made an official announcement about the 1.1 Beta, which, in official words, means that the official version is no longer in the distant future, and that everyone can try it out

Such good news, I naturally can’t hide, to send an article to everyone to introduce first. Of course, I haven’t had a chance to try out all the features in detail, so the following is a discussion of the Kotlin Evolution and Enhancement Process.

Well, in the future, we can also consider adding a 1.1 taste log every week. If there are crabs, let me eat them first, everyone watch me eat them!

0. Precautions

Although not mentioned on the official website, I want to stress that this article is about the 1.1 Beta! 1.1 Beta!!!!! 1.1 Beta!! If you install a stable version of the IDE and run Gradle 1.0.6, don’t ask “why did I get this error?”. If this happens, I don’t recommend you to try Bata. Because that’s kind of frustrating, you can tolerate that in addition to writing your own bugs there will be some bugs because of the language itself?

1. Feature overview

1.1 coroutines

I have to say, 1.1 still brought us quite a few surprises. I love it. Well, although we’ve always known that the most important thing is Coroutines, which I first heard about in Lua and Golang, which is a feature that Java doesn’t support, and it’s been back and forth with me, I was really excited to hear that Kotlin 1.1m is still considering supporting coroutines. Well, now we’re finally one step closer.

Coroutines are actually a concept that deals with concurrent scheduling, and non-blocking asynchronous calls may be familiar to those of you who have experience with dynamic language development, but new to those of us who are Java programmers in the backwoods. Tired of your new threads and callbacks? Let’s look at the code for asynchronously loading data under Kotlin 1.1:



This may seem like nothing special, but note that orginal and overlay can both be loaded asynchronously, and the applyOverlay method will be called until both are ready.

It looks very human, you don’t have to worry too much about the timing of the call, just leave it to the virtual machine. Revision 3 Coroutines for Kotlin (Revision 3), which I’ll push over time.

1.2 Type Aliases

In fact, many times we need a type to represent a variety of different meanings. For example, for a music player, in random mode, a song in the playlist has two positions: the actual position, the play position. Both positions can be represented by an int, so our code would write:



I don’t know if you noticed anything, but this method looks so bad that we can’t really figure out what kind of position it’s returning. What if you have a type alias?

It still returns an integer, but the meaning is much clearer.

Beyond this use, of course, the representation of Lambda expressions is much cleaner and more straightforward.



1.3 Bound callable references

Before we get to that, let’s look at an example:



This obviously iterates through the array and calls println to print each element.

So the question is, let’s say, instead of printing on the console, I want to print the elements to a PDF, so:



So can we simplify it?



That is, function references further support the case for binding the caller (or receiver).

1.4 Data Class can now have a dad

Previously, data classes were not allowed to inherit from other classes. Don’t ask me why, there’s no such thing as a parent. I’d rather you ask a monkey.



If you don’t stop talking, I’ll cut you

However, since 1.1, Kotlin’s creators have had the good sense to allow Data Class to have a father, which is a moving picture after all.



However, the data Class guy is still a bit of a tragedy, 1.1 also did not allow him to have children, although he can recognize the father, but without his father continue to be lonely.

1.5 Relax the subclass definition of sealed Class

A subclass of sealed Class must also be its inner class, which is becoming a thing of the past. Sealed class (sealed class) : sealed class (sealed class) There are no logical changes to this feature, but it is easier to write.

16,000 golden oil underlined

I don’t know if you’ve noticed the underline in Golang,



It’s a placeholder, and if we don’t care about it, why bother thinking of a name for it? Of course not, Kotlin 1.1 also allows you to do this:



In this example, we only care about values 2 and 4, and ignore values 1 and 3.



In this example, I only care about the second parameter, so leave the other two

Haha, it would be a lot easier if I didn’t have to think about what to name variables!

1.7 provideDelegate

I don’t know if it occurs to you that once I Delegate a member, I give control of that member to the Delegate, and if I want to listen to the member’s reads and writes, I have to do it in the Delegate.



ProvideDelegate allows you to do some work weaving into code even after configuring the Delegate, which gives the Delegate more flexibility.

1.8 Other Features

Of course, there are many more features in 1.1, including DSL scoping control, support for Java 8 methods for Kotlin collections, etc. I won’t cover them here, but you can also check them out on the official blog.

2 How do I configure the 1.1 environment

If you want crabs, you’ll have to update your Kotlin plugin:





Select 1.1 and click Check for Update.

If you are using gradle or maven build your project, then you need to add the plugin warehouse: http://dl.bintray.com/kotlin/kotlin-eap-1.1, choose the Kotlin version 1.1.0 – beta – 17.

Gradle configuration is as follows:



3 summary

Kotlin continues to iterate very quickly, with stable releases being updated almost every two months. Work on 1.1 has been known since the official release earlier this year, and the Beta is here, so we won’t have to wait too long for the official 1.1 release.

In the future, I will continue to post some articles about the new features of 1.1, I hope you pay attention to ~