plus
operator fun Point.plus(other: Point): Point {
return Point(x + other.x, y + other.y)
}
Point(1, 2) + Point(2, 3)
Slide 3
Slide 3 text
Arithmetic operations
expression function name
a + b plus
a - b minus
a * b times
a / b div
a % b mod
Slide 4
Slide 4 text
No restrictions on parameter type
operator fun Point.times(scale: Int): Point {
return Point(x * scale, y * scale)
}
Point(1, 2) * 3
Slide 5
Slide 5 text
Unary operations
Slide 6
Slide 6 text
Unary arithmetic operations
expression function name
+a unaryPlus
-a unaryMinus
!a not
++a, a++ inc
—-a, a—- dec
Slide 7
Slide 7 text
Assignment operations
Slide 8
Slide 8 text
Conventions for lists
val list = listOf(1, 2, 3)
val newList = list + 2
val mutableList = mutableListOf(1, 2, 3)
mutableList += 4
Slide 9
Slide 9 text
Prefer val to var
var list = listOf(1, 2, 3)
list += 4
list = list + 4
new list is created:
Slide 10
Slide 10 text
Equality check
Slide 11
Slide 11 text
Comparisons
"abc" < "def"
Slide 12
Slide 12 text
Comparisons
symbol translated to
a > b a.compareTo(b) > 0
a < b a.compareTo(b) < 0
a >= b a.compareTo(b) >= 0
a <= b a.compareTo(b) <= 0
Slide 13
Slide 13 text
Accessing elements by index: []
map[key]
mutableMap[key] = newValue
Slide 14
Slide 14 text
The in convention
if (key in map) { }
if (element in list) { }
Slide 15
Slide 15 text
The rangeTo convention
if (s in "abc".."def") { }
for (i in 1..2) { }
val oneTo100: IntRange = 1..100
for (i in oneTo100) { }
Slide 16
Slide 16 text
The iterator convention
operator fun CharSequence.iterator(): CharIterator
for (c in "abc") { }
Slide 17
Slide 17 text
Destructuring declarations
val (first, second) = pair
for ((key, value) in map) { }
Slide 18
Slide 18 text
Destructuring in lambdas
val map = mapOf(1 to "one", 2 to "two")
map.mapValues { (key, value) -> "$key -> $value!" }
map.mapValues { (_, value) -> "$value!" }
Slide 19
Slide 19 text
Lazy property
val lazyValue: String by lazy {
println("computed!")
"Hello"
}
fun main(args: Array) {
println(lazyValue)
println(lazyValue)
} computed!
Hello
Hello
Slide 20
Slide 20 text
Delegated properties
class C {
var prop: Type by Delegate()
}
Slide 21
Slide 21 text
Delegated properties
class C {
private val delegate = Delegate()
var prop: Type
set(value: Type) = delegate.setValue(..., value)
get() = delegate.getValue(...)
}
Slide 22
Slide 22 text
object Users : IdTable() {
val name = varchar("name", length = 50).index()
val age = integer("age")
}
class User(id: EntityID) : Entity(id) {
var name: String by Users.name
var age: Int by Users.age
}
Delegated properties in
frameworks
corresponds to a table
in the database
corresponds to a specific
entry in this table
columns
values
Slide 23
Slide 23 text
object Users : IdTable() {
val name = varchar("name", length = 50).index()
val age = integer("age")
}
class User(id: EntityID) : Entity(id) {
var name: String by Users.name
var age: Int by Users.age
}
retrieves the value and
updates it automatically
Delegated properties in
frameworks