Slide 1

Slide 1 text

Jake Wharton Android Development with Kotlin

Slide 2

Slide 2 text

Why do we need Kotlin?

Slide 3

Slide 3 text

Why do we need Kotlin? • Stuck on Java 6(.5)ish

Slide 4

Slide 4 text

Why do we need Kotlin? • Stuck on Java 6(.5)ish • No javax.time

Slide 5

Slide 5 text

Why do we need Kotlin? • Stuck on Java 6(.5)ish • No javax.time • No streams

Slide 6

Slide 6 text

Why do we need Kotlin? • Stuck on Java 6(.5)ish • No javax.time • No streams • No lambdas, method refs, non-capturing anon class

Slide 7

Slide 7 text

Why do we need Kotlin? • Stuck on Java 6(.5)ish • No javax.time • No streams • No lambdas, method refs, non-capturing anon class • No try-with-resources

Slide 8

Slide 8 text

Why do we need Kotlin? • Stuck on Java 6(.5)ish • No javax.time Use ThreeTenBP / ThreeTenABP • No streams • No lambdas, method refs, non-capturing anon class • No try-with-resources

Slide 9

Slide 9 text

Why do we need Kotlin? • Stuck on Java 6(.5)ish • No javax.time Use ThreeTenBP / ThreeTenABP • No streams Use backport or RxJava • No lambdas, method refs, non-capturing anon class • No try-with-resources

Slide 10

Slide 10 text

Why do we need Kotlin? • Stuck on Java 6(.5)ish • No javax.time Use ThreeTenBP / ThreeTenABP • No streams Use backport or RxJava • No lambdas, method refs, non-capturing anon class Use Retrolambda • No try-with-resources

Slide 11

Slide 11 text

Why do we need Kotlin? • Stuck on Java 6(.5)ish • No javax.time Use ThreeTenBP / ThreeTenABP • No streams Use backport or RxJava • No lambdas, method refs, non-capturing anon class Use Retrolambda • No try-with-resources minSdkVersion=19 or Retrolambda

Slide 12

Slide 12 text

Why do we need Kotlin? • Stuck on Java 6(.5)ish • Java language restrictions and problems

Slide 13

Slide 13 text

Why do we need Kotlin? • Java language restrictions and problems

Slide 14

Slide 14 text

Why do we need Kotlin? • Java language restrictions and problems • Inability to add methods to platform types

Slide 15

Slide 15 text

Why do we need Kotlin? • Java language restrictions and problems • Inability to add methods to platform types ("Util" hell)

Slide 16

Slide 16 text

Why do we need Kotlin? • Java language restrictions and problems • Inability to add methods to platform types ("Util" hell) • Nullability problems

Slide 17

Slide 17 text

Why do we need Kotlin? • Java language restrictions and problems • Inability to add methods to platform types ("Util" hell) • Nullability problems • Mutability problems

Slide 18

Slide 18 text

Why do we need Kotlin? • Java language restrictions and problems • Inability to add methods to platform types ("Util" hell) • Nullability problems • Mutability problems • General verbosity of common idioms

Slide 19

Slide 19 text

Why do we need Kotlin? • Stuck on Java 6(.5)ish • Java language restrictions and problems • Android API design problems

Slide 20

Slide 20 text

Why do we need Kotlin? • Android API design problems

Slide 21

Slide 21 text

Why do we need Kotlin? • Android API design problems • Inheritance party

Slide 22

Slide 22 text

Why do we need Kotlin? • Android API design problems • Inheritance party • Nullability everywhere

Slide 23

Slide 23 text

Why do we need Kotlin? • Android API design problems • Inheritance party • Nullability everywhere • Ceremony of APIs

Slide 24

Slide 24 text

Why do we need Kotlin? • Stuck on Java 6(.5)ish • Java language restrictions and problems • Android API design problems

Slide 25

Slide 25 text

Kotlin Statically typed programming language for the JVM, Android, and the browser 100% interoperable with Java™

Slide 26

Slide 26 text

Syntax Crash Course

Slide 27

Slide 27 text

Syntax Crash Course fun sum(a: Int, b: Int): Int {
 return a + b
 }

Slide 28

Slide 28 text

Syntax Crash Course fun sum(a: Int, b: Int): Int {
 return a + b
 }X

Slide 29

Slide 29 text

Syntax Crash Course fun sum(a: Int, b: Int): Int {
 return a + b
 }X

Slide 30

Slide 30 text

Syntax Crash Course fun sum(a: Int, b: Int): Int {
 return a + b
 }X

Slide 31

Slide 31 text

Syntax Crash Course fun sum(a: Int, b: Int) = a + b

Slide 32

Slide 32 text

Syntax Crash Course fun main(args: Array) {
 println("Args: $args")
 }

Slide 33

Slide 33 text

Syntax Crash Course fun main(args: Array) {
 println("Args: $args")
 }X

Slide 34

Slide 34 text

Syntax Crash Course fun main(args: Array) {
 println("Args: $args")
 }X

Slide 35

Slide 35 text

Syntax Crash Course fun main(args: Array) {
 println("Args: $args")
 }X

Slide 36

Slide 36 text

Syntax Crash Course val name = "Jake"
 val people: List = ArrayList()

Slide 37

Slide 37 text

Syntax Crash Course val name = "Jake"
 val people: List = ArrayList()

Slide 38

Slide 38 text

Syntax Crash Course val name = "Jake"
 val people: List = ArrayList()

Slide 39

Slide 39 text

Syntax Crash Course val name = "Jake"
 val people: List = ArrayList()

Slide 40

Slide 40 text

Syntax Crash Course val name = "Jake"
 val people: List = ArrayList()

Slide 41

Slide 41 text

Extension Functions

Slide 42

Slide 42 text

Extension Functions static boolean isTuesday(Date date) {
 return date.getDay() == 2;
 }

Slide 43

Slide 43 text

Extension Functions static boolean isTuesday(Date date) {
 return date.getDay() == 2;
 } boolean tuesday = DateUtils.isTuesday(date);

Slide 44

Slide 44 text

Extension Functions fun Date.isTuesday(): Boolean {
 return getDay() == 2
 }X

Slide 45

Slide 45 text

Extension Functions fun Date.isTuesday(): Boolean {
 return getDay() == 2
 }X

Slide 46

Slide 46 text

Extension Functions fun Date.isTuesday(): Boolean {
 return getDay() == 2
 }X

Slide 47

Slide 47 text

Extension Functions fun Date.isTuesday(): Boolean {
 return getDay() == 2
 }X val tuesday = date.isTuesday()

Slide 48

Slide 48 text

Extension Functions fun Date.isTuesday(): Boolean {
 return day == 2
 }X val tuesday = date.isTuesday()

Slide 49

Slide 49 text

Extension Functions fun Date.isTuesday() = day == 2 val tuesday = date.isTuesday()

Slide 50

Slide 50 text

Extension Functions // com/example/util/DateExtensions.kt fun Date.isTuesday() = day == 2 // com/example/TuesdayActivity.kt val tuesday = date.isTuesday()

Slide 51

Slide 51 text

Extension Functions // com/example/util/DateExtensions.kt fun Date.isTuesday() = day == 2 // com/example/TuesdayActivity.kt val tuesday = date.isTuesday()

Slide 52

Slide 52 text

Extension Functions // com/example/util/DateExtensions.kt fun Date.isTuesday() = day == 2 // com/example/TuesdayActivity.kt import com.example.util.isTuesday val tuesday = date.isTuesday()

Slide 53

Slide 53 text

Extension Functions // com/example/util/DateExtensions.kt fun Date.isTuesday() = day == 2 final static isTuesday(Ljava/util/Date;)Z L0 ALOAD 0 INVOKEVIRTUAL java/util/Date.getDay ()I ICONST_2 IF_ICMPNE L1 ICONST_1 GOTO L2 L1 ICONST_0 L2 IRETURN

Slide 54

Slide 54 text

Extension Functions // com/example/util/DateExtensions.kt fun Date.isTuesday() = day == 2 static boolean isTuesday(Date date) {
 return date.getDay() == 2;
 }X

Slide 55

Slide 55 text

Extension Functions // com/example/util/DateExtensions.kt // com/example/util/DateExtensionsKt.java fun Date.isTuesday() = day == 2 static boolean isTuesday(Date date) {
 return date.getDay() == 2;
 }X

Slide 56

Slide 56 text

Extension Functions // com/example/util/DateExtensions.kt fun Date.isTuesday() = day == 2 @file:JvmName("DateUtils") // com/example/util/DateExtensionsKt.java static boolean isTuesday(Date date) {
 return date.getDay() == 2;
 }X

Slide 57

Slide 57 text

Extension Functions // com/example/util/DateExtensions.kt fun Date.isTuesday() = day == 2 @file:JvmName("DateUtils") // com/example/util/DateUtils.java static boolean isTuesday(Date date) {
 return date.getDay() == 2;
 }X

Slide 58

Slide 58 text

Extension Functions fun Date.getDay() = 2

Slide 59

Slide 59 text

Extension Functions fun Date.getDay() = 2

Slide 60

Slide 60 text

Extension Functions fun Date.getDay() = 2 fun View.isAttachedToWindow() = if (Build.VERSION.SDK_INT < 19) ViewCompat.isAttachedToWindow(this) else super.isAttachedToWindow()

Slide 61

Slide 61 text

Extension Functions fun Date.getDay() = 2 fun View.isAttachedToWindowCompat() = if (Build.VERSION.SDK_INT < 19) ViewCompat.isAttachedToWindow(this) else isAttachedToWindow()

Slide 62

Slide 62 text

Extension Functions String firstName1=2cursor.getString( cursor.getColumnIndexOrThrow("first_name"));

Slide 63

Slide 63 text

Extension Functions String firstName1= null;
 int firstNameColumn = cursor.getColumnIndexOrThrow("first_name");
 if (!cursor.isNull(firstNameColumn)) {
 firstName3=2cursor.getString(firstNameColumn);
 }

Slide 64

Slide 64 text

Extension Functions String firstName1= null;
 int firstNameColumn = cursor.getColumnIndexOrThrow("first_name");
 if (!cursor.isNull(firstNameColumn)) {
 firstName3=2cursor.getString(firstNameColumn);
 } fun Cursor.getStringOrNull(columnName: String): String? {
 val index = getColumnIndexOrThrow(columnName)
 return if (isNull(index)) null else getString(index)
 }X
 fun Cursor.getString(columnName: String): String = getStringOrNull(columnName)!!

Slide 65

Slide 65 text

Extension Functions fun Cursor.getStringOrNull(columnName: String): String? {
 val index = getColumnIndexOrThrow(columnName)
 return if (isNull(index)) null else getString(index)
 }X
 fun Cursor.getString(columnName: String): String = getStringOrNull(columnName)!!

Slide 66

Slide 66 text

Extension Functions fun Cursor.getStringOrNull(columnName: String): String? {
 val index = getColumnIndexOrThrow(columnName)
 return if (isNull(index)) null else getString(index)
 }X
 fun Cursor.getString(columnName: String): String = getStringOrNull(columnName)!! val firstName = cursor.getStringOrNull("first_name")

Slide 67

Slide 67 text

Extension Functions fun Cursor.getStringOrNull(columnName: String): String? {
 val index = getColumnIndexOrThrow(columnName)
 return if (isNull(index)) null else getString(index)
 }X
 fun Cursor.getString(columnName: String): String = getStringOrNull(columnName)!! val firstName = cursor.getStringOrNull("first_name")

Slide 68

Slide 68 text

Extension Functions fun Cursor.getStringOrNull(columnName: String): String? {
 val index = getColumnIndexOrThrow(columnName)
 return if (isNull(index)) null else getString(index)
 }X
 fun Cursor.getString(columnName: String): String = getStringOrNull(columnName)!! val firstName: String? = cursor.getStringOrNull("first_name")

Slide 69

Slide 69 text

Extension Functions fun Cursor.getStringOrNull(columnName: String): String? {
 val index = getColumnIndexOrThrow(columnName)
 return if (isNull(index)) null else getString(index)
 }X
 fun Cursor.getString(columnName: String): String = getStringOrNull(columnName)!! val firstName: String? = cursor.getStringOrNull("first_name") if (firstName != null) { firstNameView.setText(firstName) }X

Slide 70

Slide 70 text

Extension Functions fun Cursor.getStringOrNull(columnName: String): String? {
 val index = getColumnIndexOrThrow(columnName)
 return if (isNull(index)) null else getString(index)
 }X
 fun Cursor.getString(columnName: String): String = getStringOrNull(columnName)!! val firstName: String? = cursor.getStringOrNull("first_name") if (firstName != null) { firstNameView.setText(firstName) // firstName not String?, now String }X

Slide 71

Slide 71 text

Extension Functions fun Cursor.getStringOrNull(columnName: String): String? {
 val index = getColumnIndexOrThrow(columnName)
 return if (isNull(index)) null else getString(index)
 }X
 fun Cursor.getString(columnName: String): String = getStringOrNull(columnName)!! val firstName: String? = cursor.getStringOrNull("first_name") firstNameView.setText(firstName ?: "Jake")

Slide 72

Slide 72 text

Extension Functions fun Cursor.getStringOrNull(columnName: String): String? {
 val index = getColumnIndexOrThrow(columnName)
 return if (isNull(index)) null else getString(index)
 }X
 fun Cursor.getString(columnName: String): String = getStringOrNull(columnName)!! val firstName = cursor.getString("first_name")

Slide 73

Slide 73 text

Extension Functions fun Cursor.getStringOrNull(columnName: String): String? {
 val index = getColumnIndexOrThrow(columnName)
 return if (isNull(index)) null else getString(index)
 }X
 fun Cursor.getString(columnName: String): String = getStringOrNull(columnName)!! val firstName: String = cursor.getString("first_name")

Slide 74

Slide 74 text

Extension Functions fun Cursor.getStringOrNull(columnName: String): String? {
 val index = getColumnIndexOrThrow(columnName)
 return if (isNull(index)) null else getString(index)
 }X
 fun Cursor.getString(columnName: String): String = getStringOrNull(columnName)!! val firstName: String = cursor.getString("first_name") firstNameView.setText(firstName)

Slide 75

Slide 75 text

Extension Functions fun Cursor.getStringOrNull(columnName: String): String? {
 val index = getColumnIndexOrThrow(columnName)
 return if (isNull(index)) null else getString(index)
 }X
 fun Cursor.getString(columnName: String): String = getStringOrNull(columnName)!! val firstName: String = cursor.getString("first_name") firstNameView.setText(firstName)

Slide 76

Slide 76 text

Function Expressions

Slide 77

Slide 77 text

Function Expressions { it.toString() }

Slide 78

Slide 78 text

Function Expressions { x, y -> x + y } { it.toString() }

Slide 79

Slide 79 text

Function Expressions { x, y -> x + y } { x: Int, y: Int -> x + y } { it.toString() } { x, y -> x + y } { x: Int, y: Int -> x + y }

Slide 80

Slide 80 text

Function Expressions { x, y -> x + y } { x: Int, y: Int -> x + y } val sum = { x: Int, y: Int -> x + y } val sum: (Int, Int) -> Int = { x, y -> x + y } { it.toString() }

Slide 81

Slide 81 text

Function Expressions { x, y -> x + y } { x: Int, y: Int -> x + y } val sum = { x: Int, y: Int -> x + y } val sum: (Int, Int) -> Int = { x, y -> x + y } { it.toString() }

Slide 82

Slide 82 text

Function Expressions { x, y -> x + y } { x: Int, y: Int -> x + y } val sum = { x: Int, y: Int -> x + y } val sum: (Int, Int) -> Int = { x, y -> x + y } { it.toString() }

Slide 83

Slide 83 text

Function Expressions { !it.isEmpty() } { it.length() > 4 } { it.matches("\\d{4}") } { luhnCheck(it) }

Slide 84

Slide 84 text

Function Expressions val notEmpty: (String) -> Boolean = { !it.isEmpty() } val atLeastFour: (String) -> Boolean = { it.length() > 4 } val fourDigits: (String) -> Boolean = { it.matches("\\d{4}") } val validCreditCard: (String) -> Boolean = { luhnCheck(it) }

Slide 85

Slide 85 text

Higher-Order Functions

Slide 86

Slide 86 text

Higher-Order Functions fun List.filter(predicate: (T) -> Boolean): List { // ...
 }X

Slide 87

Slide 87 text

Higher-Order Functions fun List.filter(predicate: (T) -> Boolean): List { // ...
 }X

Slide 88

Slide 88 text

Higher-Order Functions fun List.filter(predicate: (T) -> Boolean): List { // ...
 }X

Slide 89

Slide 89 text

Higher-Order Functions fun List.filter(predicate: (T) -> Boolean): List { // ...
 }X

Slide 90

Slide 90 text

Higher-Order Functions fun List.filter(predicate: (T) -> Boolean): List {
 val newList = ArrayList() // ...
 return newList
 }X

Slide 91

Slide 91 text

Higher-Order Functions fun List.filter(predicate: (T) -> Boolean): List {
 val newList = ArrayList()
 for (item in this) { // ...
 }Y
 return newList
 }X

Slide 92

Slide 92 text

Higher-Order Functions fun List.filter(predicate: (T) -> Boolean): List {
 val newList = ArrayList()
 for (item in this) {
 if (predicate(item)) { // ...
 }Z
 }Y
 return newList
 }X

Slide 93

Slide 93 text

Higher-Order Functions fun List.filter(predicate: (T) -> Boolean): List {
 val newList = ArrayList()
 for (item in this) {
 if (predicate(item)) {
 newList.add(item)
 }Z
 }Y
 return newList
 }X

Slide 94

Slide 94 text

Higher-Order Functions fun List.filter(predicate: (T) -> Boolean): List {
 val newList = ArrayList()
 for (item in this) {
 if (predicate(item)) {
 newList.add(item)
 }
 }
 return newList
 } val names = listOf("Jake", "Jesse", "Matt", "Alec")
 val jakes = names.filter { it == "Jake" }


Slide 95

Slide 95 text

Higher-Order Functions data class Lock(private val obj: T) {
 public fun acquire(func: (T) -> Unit) {
 synchronized (obj) {
 func(obj)
 }X
 }X
 }X

Slide 96

Slide 96 text

Higher-Order Functions data class Lock(private val obj: T) {
 public fun acquire(func: (T) -> Unit) {
 synchronized (obj) {
 func(obj)
 }X
 }X
 }X

Slide 97

Slide 97 text

Higher-Order Functions data class Lock(private val obj: T) {
 public fun acquire(func: (T) -> Unit) {
 synchronized (obj) {
 func(obj)
 }X
 }X
 }X

Slide 98

Slide 98 text

Higher-Order Functions data class Lock(private val obj: T) {
 public fun acquire(func: (T) -> Unit) {
 synchronized (obj) {
 func(obj)
 }X
 }X
 }X

Slide 99

Slide 99 text

Higher-Order Functions data class Lock(private val obj: T) {
 public fun acquire(func: (T) -> Unit) {
 synchronized (obj) {
 func(obj)
 }
 }
 }

Slide 100

Slide 100 text

Higher-Order Functions data class Lock(private val obj: T) {
 public fun acquire(func: (T) -> Unit) {
 synchronized (obj) {
 func(obj)
 }
 }
 } val readerLock = Lock(JsonReader(stream))

Slide 101

Slide 101 text

Higher-Order Functions data class Lock(private val obj: T) {
 public fun acquire(func: (T) -> Unit) {
 synchronized (obj) {
 func(obj)
 }
 }
 } val readerLock = Lock(JsonReader(stream))
 
 // Later
 readerLock.acquire { 
 println(it.readString())
 }

Slide 102

Slide 102 text

Higher-Order Functions val notEmpty: (String) -> Boolean = { !it.isEmpty() } val atLeastFour: (String) -> Boolean = { it.length() > 4 } val fourDigits: (String) -> Boolean = { it.matches("\\d{4}") } val validCreditCard: (String) -> Boolean = { luhnCheck(it) }

Slide 103

Slide 103 text

Higher-Order Functions val notEmpty: (String) -> Boolean = { !it.isEmpty() } val atLeastFour: (String) -> Boolean = { it.length() > 4 } val fourDigits: (String) -> Boolean = { it.matches("\\d{4}") } val validCreditCard: (String) -> Boolean = { luhnCheck(it) } firstName.validateWith(notEmpty) lastName.validateWith(notEmpty) username.validateWith(atLeastFour) pin.validateWith(fourDigits) creditCard.validateWith(validCreditCard) { !it.isEmpty() }

Slide 104

Slide 104 text

Higher-Order Functions firstName.validateWith { !it.isEmpty() } lastName.validateWith { !it.isEmpty() } username.validateWith { it.length() > 4 } pin.validateWith { it.matches("\\d{4}") } creditCard.validateWith { luhnCheck(it) }

Slide 105

Slide 105 text

Higher-Order Functions val notEmpty: (String) -> Boolean = { !it.isEmpty() } firstName.validateWith(notEmpty) lastName.validateWith(notEmpty) username.validateWith { it.length() > 4 } pin.validateWith { it.matches("\\d{4}") } creditCard.validateWith { luhnCheck(it) } { !it.isEmpty() }

Slide 106

Slide 106 text

Extension Function Expressions

Slide 107

Slide 107 text

Extension Function Expressions • Extension Functions — Functions added to a type without modifying the original.

Slide 108

Slide 108 text

Extension Function Expressions • Extension Functions — Functions added to a type without modifying the original. • Function Expressions — Undeclared function bodies used an as expression (i.e., as data).

Slide 109

Slide 109 text

Extension Function Expressions • Extension Functions — Functions added to a type without modifying the original. • Function Expressions — Undeclared function bodies used an as expression (i.e., as data). • Higher-Order Functions: A function which takes a function or returns a function.

Slide 110

Slide 110 text

Extension Function Expressions db.beginTransaction();
 try {
 db.delete("users", "first_name = ?", new String[] { "Jake" });
 } finally {
 db.endTransaction();
 }X

Slide 111

Slide 111 text

Extension Function Expressions db.beginTransaction();
 try {
 db.delete("users", "first_name = ?", new String[] { "Jake" });
 db.setTransactionSuccessful();
 } finally {
 db.endTransaction();
 }X

Slide 112

Slide 112 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: () -> Unit) {
 beginTransaction()
 try {
 func()
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }
 }

Slide 113

Slide 113 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: () -> Unit) {
 beginTransaction()
 try {
 func(
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 }X )

Slide 114

Slide 114 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: () -> Unit) {
 beginTransaction()
 try {
 func(
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 db.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 115

Slide 115 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: () -> Unit) {
 beginTransaction()
 try {
 func(
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 db.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 116

Slide 116 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: (SQLiteDatabase) -> Unit) {
 beginTransaction()
 try {
 func(
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 db.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 117

Slide 117 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: (SQLiteDatabase) -> Unit) {
 beginTransaction()
 try {
 func(
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 db.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 118

Slide 118 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: (SQLiteDatabase) -> Unit) {
 beginTransaction()
 try {
 func(this
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 db.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 119

Slide 119 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: (SQLiteDatabase) -> Unit) {
 beginTransaction()
 try {
 func(this
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 db.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 120

Slide 120 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: (SQLiteDatabase) -> Unit) {
 beginTransaction()
 try {
 func(this
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 it.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 121

Slide 121 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: (SQLiteDatabase) -> Unit) {
 beginTransaction()
 try {
 func(this
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 it.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 122

Slide 122 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: (SQLiteDatabase) -> Unit) {
 beginTransaction()
 try {
 func(this
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 it.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 123

Slide 123 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) {
 beginTransaction()
 try {
 func(this
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 it.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 124

Slide 124 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) {
 beginTransaction()
 try {
 func(this
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 it.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 125

Slide 125 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) {
 beginTransaction()
 try {
 this.func(
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 it.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 126

Slide 126 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) {
 beginTransaction()
 try {
 func(
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 it.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 127

Slide 127 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) {
 beginTransaction()
 try {
 func(
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 it.delete("users", "first_name = ?", arrayOf("Jake"))
 }X )

Slide 128

Slide 128 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) {
 beginTransaction()
 try {
 func()
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 delete("users", "first_name = ?", arrayOf("Jake"))
 }X

Slide 129

Slide 129 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) {
 beginTransaction()
 try {
 func()
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X db.inTransaction { 
 delete("users", "first_name = ?", arrayOf("Jake"))
 }X

Slide 130

Slide 130 text

Extension Function Expressions db.inTransaction { 
 delete("users", "first_name = ?", arrayOf("Jake"))
 }X

Slide 131

Slide 131 text

Extension Function Expressions NEW MyClassKt$deleteUsers$1 DUP ALOAD 2 INVOKESPECIAL MyClassKt$deleteUsers$1. (Ljava/lang/String;)V CHECKCAST kotlin/jvm/functions/Function1 INVOKESTATIC DbExtensionsKt.inTransaction (Landroid/database/sqlite/SQLiteDatabase;Lkotlin/jvm/functions/Function1;)V db.inTransaction { 
 delete("users", "first_name = ?", arrayOf("Jake"))
 }X

Slide 132

Slide 132 text

Extension Function Expressions DbExtensions.inTransaction(db, new Function1() {
 @Override public Unit invoke(SQLiteDatabase db) {
 db.delete("users", "first_name = ?", new String[] { "Jake "});
 return null;
 }
 }); db.inTransaction { 
 delete("users", "first_name = ?", arrayOf("Jake"))
 }X

Slide 133

Slide 133 text

Extension Function Expressions fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) {
 beginTransaction()
 try {
 func()
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X

Slide 134

Slide 134 text

Extension Function Expressions inline fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) {
 beginTransaction()
 try {
 func()
 setTransactionSuccessful()
 } finally {
 endTransaction()
 }Y
 }X

Slide 135

Slide 135 text

Extension Function Expressions L6 ALOAD 2 INVOKEVIRTUAL com/example/SQLiteDatabase.beginTransaction ()V L0 NOP L9 ALOAD 2 CHECKCAST com/example/SQLiteDatabase ASTORE 3 L10 ALOAD 3 LDC "users" LDC "first_name = ?" ICONST_1 ANEWARRAY java/lang/String DUP ICONST_0 LDC "Jake" AASTORE CHECKCAST [Ljava/lang/String; INVOKEVIRTUAL com/example/SQLiteDatabase.delete (Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V L11 L12 GETSTATIC kotlin/Unit.INSTANCE : Lkotlin/Unit; POP L13 ALOAD 2 INVOKEVIRTUAL com/example/SQLiteDatabase.setTransactionSuccessful ()V L1 ALOAD 2 INVOKEVIRTUAL com/example/SQLiteDatabase.endTransaction ()V L14 GOTO L15 L2 ASTORE 3 L3 ALOAD 2 INVOKEVIRTUAL com/example/SQLiteDatabase.endTransaction ()V

Slide 136

Slide 136 text

Extension Function Expressions db.beginTransaction();
 try {
 db.delete("users", "first_name = ?", new String[] { "Jake "});
 db.setTransactionSuccessful();
 } finally {
 db.endTransaction();
 }X

Slide 137

Slide 137 text

Extension Function Expressions db.beginTransaction();
 try {
 db.delete("users", "first_name = ?", new String[] { "Jake "});
 db.setTransactionSuccessful();
 } finally {
 db.endTransaction();
 }X db.inTransaction { 
 delete("users", "first_name = ?", arrayOf("Jake"))
 }X + inline fun =

Slide 138

Slide 138 text

Extension Function Expressions inline fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) {
 val editor = edit()
 editor.func()
 editor.apply()
 }

Slide 139

Slide 139 text

Extension Function Expressions inline fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) {
 val editor = edit()
 editor.func()
 editor.apply()
 }X preferences.edit { 
 putString("foo", "bar")
 putString("fizz", "buzz")
 remove("username")
 }X

Slide 140

Slide 140 text

Extension Function Expressions inline fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) {
 val editor = edit()
 editor.func()
 editor.apply()
 }X 
 fun SharedPreferences.Editor.set(pair: Pair) =
 putString(pair.first, pair.second) preferences.edit { 
 putString("foo", "bar")
 putString("fizz", "buzz")
 remove("username")
 }X

Slide 141

Slide 141 text

Extension Function Expressions inline fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) {
 val editor = edit()
 editor.func()
 editor.apply()
 }X 
 fun SharedPreferences.Editor.set(pair: Pair) =
 putString(pair.first, pair.second) preferences.edit {
 set("foo" to "bar")
 set("fizz" to "buzz")
 remove("username")
 }X 
 
 
 
 
 
 
 putString
 putString

Slide 142

Slide 142 text

Extension Function Expressions inline fun notification(context: Context, func: Notification.Builder.() -> Unit): Notification {
 val builder = Notification.Builder(context)
 builder.func()
 return builder.build()
 }

Slide 143

Slide 143 text

Extension Function Expressions inline fun notification(context: Context, func: Notification.Builder.() -> Unit): Notification {
 val builder = Notification.Builder(context)
 builder.func()
 return builder.build()
 } val n = notification(context) {
 setContentTitle("Hi")
 setSubText("Hello")
 }

Slide 144

Slide 144 text

Extension Function Expressions verticalLayout {
 padding = dip(30)
 editText {
 hint = "Name"
 textSize = 24f
 }
 editText {
 hint = "Password"
 textSize = 24f
 }
 button("Login") {
 textSize = 26f
 }
 }

Slide 145

Slide 145 text

Resources

Slide 146

Slide 146 text

Resources • Website and documentation
 https://kotlinlang.org/

Slide 147

Slide 147 text

Resources • Website and documentation
 https://kotlinlang.org/ • Online IDE
 http://try.kotlinlang.org/

Slide 148

Slide 148 text

Resources • Website and documentation
 https://kotlinlang.org/ • Online IDE
 http://try.kotlinlang.org/ • Kotlin Koans
 http://try.kotlinlang.org/koans

Slide 149

Slide 149 text

jakewharton jakewharton jakewharton twitter.com/ google.com/+ .com Android Development with Kotlin