Foreword |
|
xv | |
Preface |
|
xvii | |
Acknowledgments |
|
xix | |
About This Book |
|
xxi | |
About The Authors |
|
xxiv | |
About The Cover Illustration |
|
xxv | |
Part 1 Introducing Kotlin |
|
|
|
3 | (14) |
|
|
3 | (1) |
|
1.2 Kotlin's primary traits |
|
|
4 | (3) |
|
Target platforms: server-side, Android, anywhere Java runs |
|
|
4 | (1) |
|
|
5 | (1) |
|
Functional and object-oriented |
|
|
6 | (1) |
|
|
7 | (1) |
|
|
7 | (3) |
|
Kotlin on the server side |
|
|
8 | (1) |
|
|
9 | (1) |
|
1.4 The philosophy of Kotlin |
|
|
10 | (3) |
|
|
10 | (1) |
|
|
11 | (1) |
|
|
12 | (1) |
|
|
12 | (1) |
|
1.5 Using the Kotlin tools |
|
|
13 | (2) |
|
|
13 | (1) |
|
Plug-in forIntellif IDEA and Android Studio |
|
|
14 | (1) |
|
|
15 | (1) |
|
|
15 | (1) |
|
|
15 | (1) |
|
|
15 | (1) |
|
|
15 | (2) |
|
|
17 | (27) |
|
2.1 Basic elements: functions and variables |
|
|
18 | (5) |
|
|
18 | (1) |
|
|
18 | (2) |
|
|
20 | (2) |
|
Easier string formatting: string templates |
|
|
22 | (1) |
|
2.2 Classes and properties |
|
|
23 | (5) |
|
|
23 | (2) |
|
|
25 | (1) |
|
Kotlin source code layout: directories and packages |
|
|
26 | (2) |
|
2.3 Representing and handling choices: enums and "when" |
|
|
28 | (7) |
|
|
28 | (1) |
|
Using "when" to deal with enum classes |
|
|
29 | (1) |
|
Using "when" with arbitrary objects |
|
|
30 | (1) |
|
Using "when" without an argument |
|
|
31 | (1) |
|
Smart casts: combining type checks and casts |
|
|
31 | (2) |
|
Refactoring: replacing "if with "when" |
|
|
33 | (1) |
|
Blocks as branches of "if' and "when" |
|
|
34 | (1) |
|
2.4 Iterating over things: "while" and "for" loops |
|
|
35 | (4) |
|
|
35 | (1) |
|
Iterating over numbers: ranges and progressions |
|
|
36 | (1) |
|
|
37 | (1) |
|
Using "in" to check collection and range membership |
|
|
38 | (1) |
|
|
39 | (3) |
|
"try", "catch", and "finally" |
|
|
40 | (1) |
|
|
41 | (1) |
|
|
42 | (2) |
|
3 Defining and calling functions |
|
|
44 | (23) |
|
3.1 Creating collections in Kotlin |
|
|
45 | (1) |
|
3.2 Making functions easier to call |
|
|
46 | (5) |
|
|
47 | (1) |
|
|
48 | (1) |
|
Getting rid of static utility classes: top-level functions and properties |
|
|
49 | (2) |
|
3.3 Adding methods to other people's classes: extension functions and properties |
|
|
51 | (6) |
|
Imports and extension functions |
|
|
53 | (1) |
|
Calling extension functions from Java |
|
|
53 | (1) |
|
Utility functions as extensions |
|
|
54 | (1) |
|
No overriding for extension functions |
|
|
55 | (1) |
|
|
56 | (1) |
|
3.4 Working with collections: varargs, infix calls, and library support |
|
|
57 | (3) |
|
Extending the Java Collections API |
|
|
57 | (1) |
|
Varargs: functions that accept an arbitrary number of arguments |
|
|
58 | (1) |
|
Working with pairs: infix calls and destructuring declarations |
|
|
59 | (1) |
|
3.5 Working with strings and regular expressions |
|
|
60 | (4) |
|
|
60 | (1) |
|
Regular expressions and triple-quoted strings |
|
|
61 | (1) |
|
Multiline triple-quoted strings |
|
|
62 | (2) |
|
3.6 Making your code tidy: local functions and extensions |
|
|
64 | (2) |
|
|
66 | (1) |
|
4 Classes, objects, and interfaces |
|
|
67 | (36) |
|
4.1 Defining class hierarchies |
|
|
68 | (10) |
|
|
68 | (2) |
|
Open, final, and abstract modifiers: final by default |
|
|
70 | (3) |
|
^ Visibility modifiers: public by default |
|
|
73 | (2) |
|
Inner and nested classes: nested by default |
|
|
75 | (2) |
|
Sealed classes: defining restricted class hierarchies |
|
|
77 | (1) |
|
4.2 Declaring a class with nontrivial constructors or properties |
|
|
78 | (9) |
|
Initializing classes: primary constructor and initializes blocks |
|
|
79 | (2) |
|
Secondary constructors: initializing the superclass in different ways |
|
|
81 | (2) |
|
Implementing properties declared in interfaces |
|
|
83 | (2) |
|
Accessing a backing field from a getter or setter |
|
|
85 | (1) |
|
Changing accessor visibility |
|
|
86 | (1) |
|
4.3 Compiler-generated methods: data classes and class delegation |
|
|
87 | (6) |
|
|
87 | (2) |
|
Data classes: autogenerated implementations of universal methods |
|
|
89 | (2) |
|
Class delegation: using the "by" keyword |
|
|
91 | (2) |
|
4.4 The "object" keyword: declaring a class and creating an instance, combined |
|
|
93 | (8) |
|
Object declarations: singletons made easy |
|
|
93 | (3) |
|
Companion objects: a place for factory methods and static members |
|
|
96 | (2) |
|
Companion objects as regular objects |
|
|
98 | (2) |
|
Object expressions: anonymous inner classes rephrased |
|
|
100 | (1) |
|
|
101 | (2) |
|
5 Programming with lambdas |
|
|
103 | (30) |
|
5.1 Lambda expressions and member references |
|
|
104 | (9) |
|
Introduction to lambdas: blocks of code as function parameters |
|
|
104 | (1) |
|
|
105 | (1) |
|
Syntax for lambda expressions |
|
|
106 | (3) |
|
Accessing variables in scope |
|
|
109 | (2) |
|
|
111 | (2) |
|
5.2 Functional APIs for collections |
|
|
113 | (5) |
|
Essentials: filter and map |
|
|
113 | (2) |
|
"all", "any", "count", and "find": applying a predicate to a collection |
|
|
115 | (2) |
|
groupBy: converting a list to a map of groups |
|
|
117 | (1) |
|
flatMap and flatten: processing elements in nested collections |
|
|
117 | (1) |
|
5.3 Lazy collection operations: sequences |
|
|
118 | (5) |
|
Executing sequence operations: intermediate and terminal operations |
|
|
120 | (2) |
|
|
122 | (1) |
|
5.4 Using Java functional interfaces |
|
|
123 | (5) |
|
Passing a lambda as a parameter to a Java method |
|
|
124 | (2) |
|
SAM constructors: explicit conversion of lambdas to functional interfaces |
|
|
126 | (2) |
|
5.5 Lambdas with receivers: "with" and "apply" |
|
|
128 | (3) |
|
|
128 | (2) |
|
|
130 | (1) |
|
|
131 | (2) |
|
|
133 | (38) |
|
|
133 | (20) |
|
|
134 | (2) |
|
|
136 | (1) |
|
|
137 | (2) |
|
|
139 | (1) |
|
|
140 | (1) |
|
Not-null assertions: "!!" |
|
|
141 | (2) |
|
|
143 | (2) |
|
Late-initialized properties |
|
|
145 | (1) |
|
Extensions for nullable types |
|
|
146 | (2) |
|
Nullability of type parameters |
|
|
148 | (1) |
|
|
149 | (4) |
|
6.2 Primitive and other basic types |
|
|
153 | (6) |
|
Primitive types: Int, Boolean, and more |
|
|
153 | (1) |
|
Nullable primitive types: Int?, Boolean?, and more |
|
|
154 | (1) |
|
|
155 | (2) |
|
"Any" and "Any?": the root types |
|
|
157 | (1) |
|
The Unit type: Kotlin's "void" |
|
|
157 | (1) |
|
The Nothing type: "This function never returns" |
|
|
158 | (1) |
|
6.3 Collections and arrays |
|
|
159 | (11) |
|
Nullability and collections |
|
|
159 | (2) |
|
Read-only and mutable collections |
|
|
161 | (2) |
|
Kotlin collections and Java |
|
|
163 | (2) |
|
Collections as platform types |
|
|
165 | (2) |
|
Arrays of objects and primitive types |
|
|
167 | (3) |
|
|
170 | (1) |
Part 2 Embracing Kotlin |
|
171 | (142) |
|
7 Operator overloading and other conventions |
|
|
173 | (27) |
|
7.1 Overloading arithmetic operators |
|
|
174 | (6) |
|
Overloading binary arithmetic operations |
|
|
174 | (3) |
|
Overloading compound assignment operators |
|
|
177 | (1) |
|
Overloading unary operators |
|
|
178 | (2) |
|
7.2 Overloading comparison operators |
|
|
180 | (2) |
|
Equality operators: "equals" |
|
|
180 | (1) |
|
Ordering operators: compareTo |
|
|
181 | (1) |
|
7.3 Conventions used for collections and ranges |
|
|
182 | (5) |
|
Accessing elements by index: "get" and "set" |
|
|
182 | (2) |
|
|
184 | (1) |
|
|
185 | (1) |
|
The "iterator" convention for the "for" loop |
|
|
186 | (1) |
|
7.4 Destructuring declarations and component functions |
|
|
187 | (2) |
|
Destructuring declarations and loops |
|
|
188 | (1) |
|
7.5 Reusing property accessor logic: delegated properties |
|
|
189 | (10) |
|
Delegated properties: the basics |
|
|
189 | (1) |
|
Using delegated properties: lazy initialization and "by lazy( )" |
|
|
190 | (2) |
|
Implementing delegated properties |
|
|
192 | (3) |
|
Delegated-property translation rules |
|
|
195 | (1) |
|
Storing property values in a map |
|
|
196 | (1) |
|
Delegated properties in frameworks |
|
|
197 | (2) |
|
|
199 | (1) |
|
8 Higher-order functions: lambdas as parameters and return values |
|
|
200 | (23) |
|
8.1 Declaring higher-order functions |
|
|
201 | (10) |
|
|
201 | (1) |
|
Calling functions passed as arguments |
|
|
202 | (2) |
|
Using function types from Java |
|
|
204 | (1) |
|
Default and null values for parameters with function types |
|
|
205 | (2) |
|
Returning functions from functions |
|
|
207 | (2) |
|
Removing duplication through lambdas |
|
|
209 | (2) |
|
8.2 Inline functions: removing the overhead of lambdas |
|
|
211 | (6) |
|
|
211 | (2) |
|
Restrictions on inline functions |
|
|
213 | (1) |
|
Inlining collection operations |
|
|
214 | (1) |
|
Deciding when to declare functions as inline |
|
|
215 | (1) |
|
Using in lined lambdas for resource management |
|
|
216 | (1) |
|
8.3 Control flow in higher-order functions |
|
|
217 | (4) |
|
Return statements in lambdas: return from an enclosing function |
|
|
217 | (1) |
|
Returning from lambdas: return with a label |
|
|
218 | (2) |
|
Anonymous functions: local returns by default |
|
|
220 | (1) |
|
|
221 | (2) |
|
|
223 | (31) |
|
9.1 Generic type parameters |
|
|
224 | (6) |
|
Generic functions and properties |
|
|
224 | (2) |
|
Declaring generic classes |
|
|
226 | (1) |
|
Type parameter constraints |
|
|
227 | (2) |
|
Making type parameters non-null |
|
|
229 | (1) |
|
9.2 Generics at runtime: erased and reified type parameters |
|
|
230 | (7) |
|
Generics at runtime: type checks and casts |
|
|
230 | (3) |
|
Declaring functions with reified type parameters |
|
|
233 | (2) |
|
Replacing class references with reified type parameters |
|
|
235 | (1) |
|
Restrictions on reified type parameters |
|
|
236 | (1) |
|
9.3 Variance: generics and subtyping |
|
|
237 | (15) |
|
Why variance exists: passing an argument to a function |
|
|
237 | (1) |
|
Classes, types, and subtypes |
|
|
238 | (2) |
|
Covariance: preserved subtyping relation |
|
|
240 | (4) |
|
Contravariance: reversed subtyping relation |
|
|
244 | (2) |
|
Use-site variance: specifying variance for type occurrences |
|
|
246 | (2) |
|
Star projection: using instead of a type argument |
|
|
248 | (4) |
|
|
252 | (2) |
|
10 Annotations and reflection |
|
|
254 | (28) |
|
10.1 Declaring and applying annotations |
|
|
255 | (9) |
|
|
255 | (1) |
|
|
256 | (2) |
|
Using annotations to customize JSON serialization |
|
|
258 | (2) |
|
|
260 | (1) |
|
Meta-annotations: controlling how an annotation is processed |
|
|
261 | (1) |
|
Classes as annotation parameters |
|
|
262 | (1) |
|
Generic classes as annotation parameters |
|
|
263 | (1) |
|
10.2 Reflection: introspecting Kotlin objects at runtime |
|
|
264 | (17) |
|
The Kotlin reflection API: KClass, KCallable, KFunction, and KProperty |
|
|
265 | (3) |
|
Implementing object serialization using reflection |
|
|
268 | (2) |
|
Customizing serialization with annotations |
|
|
270 | (3) |
|
JSON parsing and object deserialization |
|
|
273 | (4) |
|
Final deserialization step: callBy() and creating objects using reflection |
|
|
277 | (4) |
|
|
281 | (1) |
|
|
282 | (31) |
|
|
283 | (5) |
|
The concept of domain-specific languages |
|
|
284 | (1) |
|
|
285 | (1) |
|
|
286 | (1) |
|
Building HTML with an internal DSL |
|
|
287 | (1) |
|
11.2 Building structured APIs: lambdas with receivers in DSLs |
|
|
288 | (11) |
|
Lambdas with receivers and extension function types |
|
|
288 | (4) |
|
Using lambdas with receivers in HTML builders |
|
|
292 | (4) |
|
Kotlin builders: enabling abstraction and reuse |
|
|
296 | (3) |
|
11.3 More flexible block nesting with the "invoke" convention |
|
|
299 | (4) |
|
The "invoke" convention: objects callable as functions |
|
|
299 | (1) |
|
The "invoke" convention and functional types |
|
|
300 | (1) |
|
The "invoke" convention in DSLs: declaring dependencies in Cradle |
|
|
301 | (2) |
|
11.4 Kotlin DSLs in practice |
|
|
303 | (7) |
|
Chaining infix calls: "should" in test frameworks |
|
|
303 | (2) |
|
Defining extensions on primitive types: handling dates |
|
|
305 | (1) |
|
Member extension functions: internal DSL for SQL |
|
|
306 | (3) |
|
Anko: creating Android UIs dynamically |
|
|
309 | (1) |
|
|
310 | (3) |
Appendix A Building Kotlin projects |
|
313 | (4) |
Appendix B Documenting Kotlin code |
|
317 | (3) |
Appendix C The Kotlin ecosystem |
|
320 | (3) |
Index |
|
323 | |