Ever wonder what this "new" Kotlin thing is? Curious what the syntax looks like? Unsure how to implement this at your own company? Or do you just want to know what Nick and Cody's favorite things are about this language?
All that and (maybe) more are revealed in Privet Kotlin.
29. class Address {
var name: String = ...
var street: String = ...
var city: String = ...
var state: String? = ...
var zip: String = ...
}
Properties
30. fun copyAddress(address: Address): Address {
val result = Address()
result.name = address.name
result.street = address.street
return result
}
Properties
39. var max = a
if (a < b) max = b
Control Flow - If Expression
40. var max: Int
if (a > b) {
max = a
} else {
max = b
}
Control Flow - If Expression
41. val max = if (a > b) a else b
Control Flow - If Expression
42. val max = if (a > b) {
print("Returns a")
a
} else {
print("Returns b")
b
}
Control Flow - If Expression
43. when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> {
print(“x is neither 1 nor 2")
}
}
When Expression - Replaces Switch
44. when (x) {
1, 2 -> print("x == 1 or a == 2")
else -> print(“x is neither 1 nor 2”)
}
When Expression - Replaces Switch
45. when (x) {
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is valid")
!in 10..20 -> print("x is outside the range")
else -> print("none of the above")
}
When Expression - Replaces Switch
46. when {
x.isOdd() -> print("x is odd")
x.isEven() -> print("x is even")
else -> print("x is funny")
}
When Expression - Replaces If/Else If
47. for (item in collection) {
print(item)
}
For Loops
48. for (i in array.indices) {
print(array[i])
}
For Loops
49. for ((index, value) in array.withIndex()) {
println("the element at $index is $value")
}
For Loops
50. 2. Getting Started
3. Null Safety
4. Our Favorite Things About Kotlin
5. Kotlin At Work
1. Fun Facts
51. 1. Fun Facts
2. Getting Started
4. Our Favorite Things About Kotlin
5. Kotlin At Work
3. Null Safety
58. val length = s!!.length
NonNull Assert
kotlin.KotlinNullPointerException
at part2.delegation.interceptor.LoggerList.add(LoggerList.kt:39)
(don’t do this)
59. val length = s!!.length
val length = s?.length // Int?
NonNull Assert
Safe call, returns optional
kotlin.KotlinNullPointerException
at part2.delegation.interceptor.LoggerList.add(LoggerList.kt:39)
(don’t do this)
60. val length = s?.length ?: 0 // Int
Safe Call and Elvis Operator
61. val length = s?.length ?: 0 // Int
Safe Call and Elvis Operator
Other helpful operators
with, apply, let, run
s?.let {
val length = it.length // Int
// Do stuff with length
}
62. What about variables that become null?
What about variables that become non-null?
63. Mutable
Nullable
String s; var s: String?
Mutable
NonNull
String s; var s: String
Immutable
Nullable
final String s; val s: String?
Immutable
NonNull
final String s; val s: String
64. 2. Getting Started
3. Null Safety
4. Our Favorite Things About Kotlin
5. Kotlin At Work
1. Fun Facts
65. 1. Fun Facts
2. Getting Started
3. Null Safety
5. Kotlin At Work
4. Our Favorite Things About Kotlin
67. Data Classes
We frequently create classes whose main purpose is to
hold data. In such a class some standard functionality
and utility functions are often mechanically derivable
from the data. In Kotlin, this is called a data class.
70. val user = User(“Jane Smith", 27)
user.name
user.age
user.toString()
user.hashCode()
user.equals()
Data Classes
71. val jack = User(name = "Jack", age = 1)
val olderJack = jack.copy(age = 2)
Data Classes
72. val jane = User("Jane", 35)
val (name, age) = jane
println("$name, $age years of age")
Data Classes
73. Sealed Classes
Sealed classes are used for representing restricted
class hierarchies, when a value can have one of the
types from a limited set, but cannot have any other type.
They are, in a sense, an extension of enum classes: the
set of values for an enum type is also restricted, but
each enum constant exists only as a single instance,
whereas a subclass of a sealed class can have multiple
instances which can contain state.
74. sealed class Expr {
data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()
}
Sealed Classes
75. Sealed Classes
fun eval(expr: Expr): Double = when(expr) {
is Const -> expr.number
is Sum -> eval(expr.e1) + eval(expr.e2)
NotANumber -> Double.NaN
}
77. A higher-order function is a function
that takes functions as parameters,
or returns a function.
fun transform1Through5(transform: (Int) -> Int) {
for (int in 1..5) {
println(transform(int))
}
}
78. fun transform1Through5(transform: (Int) -> Int) {
for (int in 1..5) {
println(transform(int))
}
}
Method Reference
class NumberDoubler {
fun run(int: Int) = int * 2
}
fun doubleNumbersMethodReference() {
val numberDoubler = NumberDoubler()
transform1Through5(numberDoubler::run)
}
95. Kotlin In Production
• Determine code style rules as a team.
• Find the path of least resistance.
• Java-style Kotlin in the beginning.
• Focus on continuous improvement.
@