没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Modern Web Development with Kotlin
A concise and practical step-by-step guide
Denis Kalinin
This book is for sale at http://leanpub.com/modern-web-development-with-kotlin
This version was published on 2017-06-15
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
© 2016 - 2017 Denis Kalinin
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Build tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Command line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Gradle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Editing source files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Using Atom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Using IntelliJ IDEA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Language fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Using the REPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Defining values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Type hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Nullable types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Defining classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Defining objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Type parametrization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Extension functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Packages and imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Loops and conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
String templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Preface
On 18 March 2014, Java 8 was finally released, and this event marked an important point in the
history of programming languages. Java had finally got function literals! There were many other
improvements, and all of this basically put to rest myths about Java being outdated. It was obvious
to everyone that this release was going to be a tremendous success, and the language landscape was
changing for good.
Interestingly, while presenting an upcoming version, Brian Goetz, the Java language architect, noted
that even with all these shiny new features, the language still felt like Java. It was a relief for many
developers who didn’t want to feel at a loss with once a very familiar language. At the same time, it
meant that for numerous Web developers, Java was going to stay something relatively heavyweight
or even alien. As a result, even now, former Ruby or PHP programmers are much more likely to
switch to Scala, another statically-typed JVM language, than Java 8.
So, what about Kotlin? It doesn’t seem to be eager to introduce its own build tools or frameworks.
Instead, it aims at reusing existing Java libraries and integrating into already established workflows.
In particular, throughout this book, we will be using Gradle - a popular build tool initially developed
for Java. Generally speaking, Kotlin is quite flexible when it comes to build tools, so you can use
Maven or even Ant if you need to.
As a new language developed literally from scratch, Kotlin is also free from legacy problems. As
a result, there are quite a few things that it does differently than Java even in supposedly similar
situations.
For example, the if statement in Java allows to make the control flow depend on a certain condition:
1 List<Integer> ints = null;
2 if (checkCondition()) {
3 ints = generateList();
4 } else {
5 ints = Collections.emptyList();
6 }
Since if statements don’t return any values, we have to define a variable and put the assignments
in both branches. In Kotlin, if is an expression, so ints can be defined as a constant and initialized
immediately:
Preface 2
1 val ints = if (checkCondition()) {
2 generateList()
3 } else {
4 emptyList()
5 }
As an additional bonus, another Kotlin feature called type inference liberates us from declaring ints
as List<Integer>.
Similarly, try/catch blocks in Java don’t return any values, so you have to declare necessary
variables before these blocks. Add the requirement to always catch or throw checked exceptions,
and you’ll get a recipe for crafting complex and difficult to maintain code. As you will see later in
this book, with Kotlin you don’t actually need to use try/catch blocks at all! Instead, it’s usually
better to wrap dangerous logic and return an Either object:
1 val resultT = eitherTry {
2 // invoke some dangerous code
3 }
This is only a fraction of features which enable Kotlin developers to write concise and easily readable
code. Of course, we will be discussing these features in detail throughout the book.
Since Kotlin tries to fit into the existing Java ecosystem, I decided to take Vert.x - a primarily Java
framework - and use it for developing our sample Web application. Basing our narrative on Vert.x
1
allows us to experience first-hand what it is like to use existing libraries in Kotlin.
Vert.x, however, is quite an unusual beast and differs from traditional frameworks in multiple ways.
First, it’s completely asynchronous and in this respect similar to NodeJS. Instead of blocking the
computation until the result becomes available, Vert.x uses callbacks:
1 router.get("/").handler { routingContext ->
2 routingContext.response().end("Hello World")
3 }
As a modern programming language, Kotlin comes with a first-class support for lambdas (function
literals) and therefore, allows to work with Vert.x in a pleasant, almost DSL-like way.
Second, Vert.x is not based on Servlets, which makes it a good choice for people without Java
background, especially Web developers. The fact that Vert.x comes with an embedded Web server
enables simple deployment and easy integration with frontend tools.
Writing a Web application usually involves using many additional tools. While Vert.x provides a lot
out of the box, it doesn’t provide everything we need. As a result, throughout the book, we will be
1
http://vertx.io/
剩余32页未读,继续阅读
资源评论
- yhylsso2017-09-13骗分的大家不要下载
信息就是力量
- 粉丝: 3
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功