Slide 1

Slide 1 text

@skentphd Kotlin & Swift Convergent Evolution?

Slide 2

Slide 2 text

@skentphd Kotlin & Swift Convergent Evolution? Simultaneous Invention!

Slide 3

Slide 3 text

@skentphd Me Stuart Kent Detroit Labs @skentphd

Slide 4

Slide 4 text

@skentphd Me Stuart Kent Detroit Labs @skentphd

Slide 5

Slide 5 text

@skentphd Me Stuart Kent Detroit Labs @skentphd @skentphd

Slide 6

Slide 6 text

@skentphd Me Android Java iOS Swift Kotlin 2014 2015 2016 2017

Slide 7

Slide 7 text

@skentphd Me Android Java iOS Swift Kotlin 2014 2015 2016 2017

Slide 8

Slide 8 text

@skentphd Me Android Java iOS Swift Kotlin 2014 2015 2016 2017

Slide 9

Slide 9 text

@skentphd Me Android Java iOS Swift Kotlin 2014 2015 2016 2017

Slide 10

Slide 10 text

@skentphd You • 90% heard of Kotlin • 50% used Kotlin • 15% written Kotlin on Android • 10% written Kotlin in production

Slide 11

Slide 11 text

@skentphd You • 90% heard of Kotlin • 50% used Kotlin • 15% written Kotlin on Android • 10% written Kotlin in production

Slide 12

Slide 12 text

@skentphd Android & iOS

Slide 13

Slide 13 text

@skentphd Commonalities • Lifecycle • Networking • Accessibility • Background processing • Security

Slide 14

Slide 14 text

@skentphd Convergences • Platform-agnostic architectures: MVP, MVVM

Slide 15

Slide 15 text

@skentphd Convergences • Platform-agnostic architectures: MVP, MVVM • Layout components: • LinearLayout <-> UIStackView • ConstraintLayout <-> AutoLayout

Slide 16

Slide 16 text

@skentphd Convergences • Platform-agnostic architectures: MVP, MVVM • Layout components: • LinearLayout <-> UIStackView • ConstraintLayout <-> AutoLayout • Development languages

Slide 17

Slide 17 text

@skentphd Claim It's never been easier for a native Android developer to learn native iOS development.

Slide 18

Slide 18 text

@skentphd Calls To Action As Android developers, you should consider • learning Kotlin; then • learning Swift & iOS $

Slide 19

Slide 19 text

@skentphd Kotlin & Swift

Slide 20

Slide 20 text

@skentphd Kotlin & Swift • Strikingly similar principles and implementations: • Accessibility • Expressivity • Safety • Developed ~simultaneously but independently

Slide 21

Slide 21 text

@skentphd Kotlin & Swift: Accessibility • Both: "vanilla" syntax • Both: gradual adoption via interoperability • Both: excellent documentation • Kotlin: semi-automatic migration • Swift: manual migration

Slide 22

Slide 22 text

@skentphd Kotlin & Swift: Accessibility • Both: "vanilla" syntax • Both: gradual adoption via interoperability • Both: excellent documentation • Kotlin: semi-automatic migration • Swift: manual migration

Slide 23

Slide 23 text

@skentphd Basic Syntax • Both: nothing too weird; "braces not spaces"

Slide 24

Slide 24 text

@skentphd Kotlin 1.1.4 // Declarations val summit = "Android Summit" // Declarations let summit = "Android Summit" Swift 4

Slide 25

Slide 25 text

@skentphd Kotlin 1.1.4 // Declarations val summit = "Android Summit" fun greet(name: String): String { return "Hello, \{name}!" } // Declarations let summit = "Android Summit" func greet(name: String) -> String { return "Hello, \(name)!" } Swift 4

Slide 26

Slide 26 text

@skentphd Kotlin 1.1.4 // Declarations val summit = "Android Summit" fun greet(name: String): String { return "Hello, \{name}!" } // Usage greet(name = summit) // "Hello, Android Summit!" // Declarations let summit = "Android Summit" func greet(name: String) -> String { return "Hello, \(name)!" } // Usage greet(name: summit) // "Hello, Android Summit!" Swift 4

Slide 27

Slide 27 text

@skentphd Kotlin // Declarations val summit = "Android Summit" var spaceCount = 0 var letterCount = 0 // Usage for (char in summit) { if (char == ' ') { spaceCount += 1 } else { letterCount += 1 } } spaceCount // 1 letterCount // 13 // Declarations let summit = "Android Summit" var spaceCount = 0 var letterCount = 0 // Usage for char in summit { if char == " " { spaceCount += 1 } else { letterCount += 1 } } spaceCount // 1 letterCount // 13 Swift

Slide 28

Slide 28 text

@skentphd Kotlin // Declarations val summit = "Android Summit" var spaceCount = 0 var letterCount = 0 // Usage for (char in summit) { if (char == ' ') { spaceCount += 1 } else { letterCount += 1 } } spaceCount // 1 letterCount // 13 // Declarations let summit = "Android Summit" var spaceCount = 0 var letterCount = 0 // Usage for char in summit { if char == " " { spaceCount += 1 } else { letterCount += 1 } } spaceCount // 1 letterCount // 13 Swift

Slide 29

Slide 29 text

@skentphd Kotlin // Declarations interface Listener { fun notify() } // Declarations protocol Listener { func notify() } Swift

Slide 30

Slide 30 text

@skentphd Kotlin // Declarations interface Listener { fun notify() } class AnyClass : Listener { override fun notify() { // Logic } } // Declarations protocol Listener { func notify() } class AnyClass : Listener { func notify() { // Logic } } Swift

Slide 31

Slide 31 text

@skentphd Kotlin // Declarations interface Listener { fun notify() } class AnyClass : Listener { override fun notify() { // Logic } } // Usage val anyClass: Listener = AnyClass() anyClass.notify() // Declarations protocol Listener { func notify() } class AnyClass : Listener { func notify() { // Logic } } // Usage let anyClass: Listener = AnyClass() anyClass.notify() Swift

Slide 32

Slide 32 text

@skentphd Anticlimactic = Awesome

Slide 33

Slide 33 text

@skentphd Kotlin & Swift: Expressivity • Both: extension of closed source types • Both: fluent collection operators • Both: defaults shrink API surface areas • Both: algebraic data types • Both: first class functions • Both: amplify signal; reduce noise (boilerplate--)

Slide 34

Slide 34 text

@skentphd Kotlin & Swift: Expressivity • Both: extension of closed source types • Both: fluent collection operators • Both: defaults shrink API surface areas • Both: algebraic data types • Both: first class functions • Both: amplify signal; reduce noise (boilerplate--)

Slide 35

Slide 35 text

@skentphd Kotlin & Swift: Expressivity • Both: extension of closed source types • Both: fluent collection operators • Both: defaults shrink API surface areas • Both: algebraic data types • Both: first class functions • Both: amplify signal; reduce noise (boilerplate--)

Slide 36

Slide 36 text

@skentphd Extensions • Both: more discoverable utility methods • Both: fluent expression of domain concepts • Both: completeness of standard library • Both: minimize cross-platform differences

Slide 37

Slide 37 text

@skentphd Extensions • Both: more discoverable utility methods • Both: fluent expression of domain concepts • Both: completeness of standard library • Both: minimize cross-platform differences

Slide 38

Slide 38 text

@skentphd Java 7 // Declarations public final class PasswordUtil { public static boolean isValidPassword(String candidate) { // Logic } private PasswordUtil() { // No instances } }

Slide 39

Slide 39 text

@skentphd Java 7 // Declarations public final class PasswordUtil { public static boolean isValidPassword(String candidate) { // Logic } private PasswordUtil() { // No instances } } // Usage if (PasswordUtil.isValidPassword("swordfish")) { // I have to *know* this exists! // Conditional logic }

Slide 40

Slide 40 text

@skentphd Kotlin // Declarations fun String.isValidPassword():Boolean { return this == "swordfish" } // Declarations extension String { func isValidPassword() -> Bool { return self == "swordfish" } } Swift

Slide 41

Slide 41 text

@skentphd Kotlin // Declarations fun String.isValidPassword():Boolean { return this == "swordfish" } // Declarations extension String { func isValidPassword() -> Bool { return self == "swordfish" } } Swift

Slide 42

Slide 42 text

@skentphd Kotlin // Declarations fun String.isValidPassword():Boolean { return this == "swordfish" } // Declarations extension String { func isValidPassword() -> Bool { return self == "swordfish" } } Swift

Slide 43

Slide 43 text

@skentphd Kotlin // Declarations fun String.isValidPassword():Boolean { return this == "swordfish" } // Usage if ("swordfish".isValidPassword()) { // Conditional logic } // Declarations extension String { func isValidPassword() -> Bool { return self == "swordfish" } } // Usage if "swordfish".isValidPassword() { // Conditional logic } Swift

Slide 44

Slide 44 text

@skentphd Collection Operations • Both: readable -> understandable • Both: flexible -> maintainable • Both: robust • Java: streams available in 8+ • Android: streams available for minSdk 24+

Slide 45

Slide 45 text

@skentphd Collection Operations • Both: readable -> understandable • Both: flexible -> maintainable • Both: robust • Java: streams available in 8+ • Android: streams available for minSdk 24+

Slide 46

Slide 46 text

@skentphd Java // Declarations List list = Arrays.asList("1", "2", "3", "4"); String joinedList = ""; // Usage for (int index = 0; index > list.size(); index++) { joinedList += list.get(index); if (index < list.size() - 1) { joinedList += ", "; } } joinedList // "1, 2, 3, 4"

Slide 47

Slide 47 text

@skentphd Kotlin // Declarations val list = listOf("1", "2", "3", "4") // Usage list.joinToString() // "1, 2, 3, 4" // Declarations let list = ["1", "2", "3", "4"] // Usage list.joined(separator: ", ") // "1, 2, 3, 4" Swift

Slide 48

Slide 48 text

@skentphd Kotlin // Declarations val orders: List // Usage orders .map { it.dateString } .filter { it < "2017-01-01" } .sorted() .reversed() .drop(10) .take(5) // Declarations let orders: = [Order] // Usage orders .map { $0.dateString } .filter { $0 < "2017-01-01" } .sorted() .reversed() .dropFirst(10) .prefix(5) Swift

Slide 49

Slide 49 text

@skentphd Kotlin // Declarations val orders: List // Usage orders .map { it.dateString } .filter { it < "2017-01-01" } .sorted() .reversed() .drop(10) .take(5) // Declarations let orders: = [Order] // Usage orders .map { $0.dateString } .filter { $0 < "2017-01-01" } .sorted() .reversed() .dropFirst(10) .prefix(5) Swift

Slide 50

Slide 50 text

@skentphd Kotlin // Declarations val orders: List // Usage orders .map { it.dateString } .filter { it < "2017-01-01" } .sorted() .reversed() .drop(10) .take(5) // Declarations let orders: = [Order] // Usage orders .map { $0.dateString } .filter { $0 < "2017-01-01" } .sorted() .reversed() .dropFirst(10) .prefix(5) Swift

Slide 51

Slide 51 text

@skentphd Kotlin // Declarations val orders: List // Usage orders .map { it.dateString } .filter { it < "2017-01-01" } .sorted() .reversed() .drop(10) .take(5) // Declarations let orders: = [Order] // Usage orders .map { $0.dateString } .filter { $0 < "2017-01-01" } .sorted() .reversed() .dropFirst(10) .prefix(5) Swift

Slide 52

Slide 52 text

@skentphd Kotlin // Declarations val orders: List // Usage orders .map { it.dateString } .filter { it < "2017-01-01" } .sorted() .reversed() .drop(10) .take(5) // Declarations let orders: = [Order] // Usage orders .map { $0.dateString } .filter { $0 < "2017-01-01" } .sorted() .reversed() .dropFirst(10) .prefix(5) Swift

Slide 53

Slide 53 text

@skentphd Kotlin // Declarations val orders: List // Usage orders .map { it.dateString } .filter { it < "2017-01-01" } .sorted() .reversed() .drop(10) .take(5) // Declarations let orders: = [Order] // Usage orders .map { $0.dateString } .filter { $0 < "2017-01-01" } .sorted() .reversed() .dropFirst(10) .prefix(5) Swift

Slide 54

Slide 54 text

@skentphd Kotlin // Declarations val orders: List // Usage orders .map { it.dateString } .filter { it < "2017-01-01" } .sorted() .reversed() .drop(10) .take(5) // Declarations let orders: = [Order] // Usage orders .map { $0.dateString } .filter { $0 < "2017-01-01" } .sorted() .reversed() .dropFirst(10) .prefix(5) Swift

Slide 55

Slide 55 text

@skentphd Kotlin // Declarations val orders: List // Usage orders .map { it.dateString } .filter { it < "2017-01-01" } .sorted() .reversed() .drop(10) .take(5) // Declarations let orders: = [Order] // Usage orders .map { $0.dateString } .filter { $0 < "2017-01-01" } .sorted() .reversed() .dropFirst(10) .prefix(5) Swift

Slide 56

Slide 56 text

@skentphd Default Parameter Values • Both: improve call-site clarity • Both: shrink & simplify utility APIs • Both: replace builders • Both: allow cleaner manual dependency injection

Slide 57

Slide 57 text

@skentphd Default Parameter Values • Both: improve call-site clarity • Both: shrink & simplify utility APIs • Both: replace builders • Both: allow cleaner manual dependency injection

Slide 58

Slide 58 text

@skentphd Kotlin fun Iterable.joinToString( separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String { /* Logic */ }

Slide 59

Slide 59 text

@skentphd Kotlin fun Iterable.joinToString( separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String { /* Logic */ }

Slide 60

Slide 60 text

@skentphd Kotlin fun Iterable.joinToString( separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String { /* Logic */ }

Slide 61

Slide 61 text

@skentphd Kotlin fun Iterable.joinToString( separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String { /* Logic */ } listOf(1, 2, 3, 4).joinToString() // "1, 2, 3, 4" listOf(1, 2, 3, 4).joinToString(separator = " & ") // "1 & 2 & 3 & 4" listOf(1, 2, 3, 4).joinToString(prefix = "(", postfix = ")") // "(1, 2, 3, 4)"

Slide 62

Slide 62 text

@skentphd Kotlin fun Iterable.joinToString( separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String { /* Logic */ } listOf(1, 2, 3, 4).joinToString() // "1, 2, 3, 4" listOf(1, 2, 3, 4).joinToString(separator = " & ") // "1 & 2 & 3 & 4" listOf(1, 2, 3, 4).joinToString(prefix = "(", postfix = ")") // "(1, 2, 3, 4)" Java How many methods?

Slide 63

Slide 63 text

@skentphd Kotlin fun Iterable.joinToString( separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String { /* Logic */ } listOf(1, 2, 3, 4).joinToString() // "1, 2, 3, 4" listOf(1, 2, 3, 4).joinToString(separator = " & ") // "1 & 2 & 3 & 4" listOf(1, 2, 3, 4).joinToString(prefix = "(", postfix = ")") // "(1, 2, 3, 4)" Java 26 = 64 methods... plus name collisions!

Slide 64

Slide 64 text

@skentphd Swift extension Collection where Element: CustomStringConvertible { func joinToString( separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Element) -> String)? = nil) -> String { /* Logic */ } }

Slide 65

Slide 65 text

@skentphd Swift extension Collection where Element: CustomStringConvertible { func joinToString( separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Element) -> String)? = nil) -> String { /* Logic */ } }

Slide 66

Slide 66 text

@skentphd Swift extension Collection where Element: CustomStringConvertible { func joinToString( separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Element) -> String)? = nil) -> String { /* Logic */ } }

Slide 67

Slide 67 text

@skentphd Swift extension Collection where Element: CustomStringConvertible { func joinToString( separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Element) -> String)? = nil) -> String { /* Logic */ } } [1, 2, 3, 4].joinToString() // "1, 2, 3, 4" [1, 2, 3, 4].joinToString(separator: " & ") // "1 & 2 & 3 & 4" [1, 2, 3, 4].joinToString(prefix: "(", postfix: ")") // "(1, 2, 3, 4)"

Slide 68

Slide 68 text

@skentphd Kotlin & Swift: Safety • Both: statically typed • Both: force developers to confront nullability • Both: force developers to confront mutability • Kotlin: immutable views of mutable collections • Kotlin: data classes with synthetic copy methods • Swift: custom value types

Slide 69

Slide 69 text

@skentphd Kotlin & Swift: Safety • Both: statically typed • Both: force developers to confront nullability • Both: force developers to confront mutability • Kotlin: immutable views of mutable collections • Kotlin: data classes with synthetic copy methods • Swift: custom value types

Slide 70

Slide 70 text

@skentphd Explicit Nullability

Slide 71

Slide 71 text

@skentphd Java // Declarations String s1; // No nullability information by default // Usage s1.length(); // Could evaluate to a number; could crash the entire app

Slide 72

Slide 72 text

"I call it my billion-dollar mistake. It was the invention of the null reference in 1965."

Slide 73

Slide 73 text

@skentphd Java // Declarations @NonNull String s1; // Opt-in nullability information @Nullable String s2; // Opt-in nullability information

Slide 74

Slide 74 text

@skentphd Java // Declarations @NonNull String s1; // Opt-in nullability information @Nullable String s2; // Opt-in nullability information // Usage s1.length(); // Presumed safe; no warning s2.length(); // Possibly unsafe; warning only if (s2 != null) { s2.length(); // Now safe(ish); no warning }

Slide 75

Slide 75 text

"Remove barriers that make it hard to make better choices."

Slide 76

Slide 76 text

"The trillion dollar mistake: assuming the billion dollar mistake of null is its existence, and not the absence of support in the type system."

Slide 77

Slide 77 text

@skentphd Kotlin // Declarations var s1: String = "Hello, world!" var s2: String? = "Hello, world!" // Declarations var s1: String = "Hello, world!" var s2: String? = "Hello, world!" Swift

Slide 78

Slide 78 text

@skentphd Kotlin // Declarations var s1: String = "Hello, world!" var s2: String? = "Hello, world!" // Declarations var s1: String = "Hello, world!" var s2: String? = "Hello, world!" Swift

Slide 79

Slide 79 text

@skentphd Kotlin // Declarations var s1: String = "Hello, world!" var s2: String? = "Hello, world!" // Usage s1 = null // Does not compile! s2 = null // Compiles // Declarations var s1: String = "Hello, world!" var s2: String? = "Hello, world!" // Usage s1 = nil // Does not compile! s2 = nil // Compiles Swift

Slide 80

Slide 80 text

@skentphd Kotlin // Declarations var s1: String = "Hello, world!" var s2: String? = "Hello, world!" // Usage s1.first() // Compiles s2.first() // Does not compile! // Declarations var s1: String = "Hello, world!" var s2: String? = "Hello, world!" // Usage s1.first // Compiles s2.first // Does not compile! Swift

Slide 81

Slide 81 text

@skentphd Kotlin // Declarations var s1: String = "Hello, world!" var s2: String? = "Hello, world!" // Usage s1.first() // Compiles if (s2 != null) { s2!!.first() // Compiles } s2?.first() // Compiles // Declarations var s1: String = "Hello, world!" var s2: String? = "Hello, world!" // Usage s1.first // Compiles if s2 != nil { s2!.first // Compiles } s2?.first // Compiles Swift

Slide 82

Slide 82 text

@skentphd Final Thoughts

Slide 83

Slide 83 text

@skentphd Summary • Kotlin and Swift are fundamentally similar • Kotlin and Swift are functionally similar • Learning and developing with both is viable* *but not the only option!

Slide 84

Slide 84 text

@skentphd Tactics • exercism.io language tracks • Official documentation • nilhcem.com/swift-is-like-kotlin/ • Spy on peer PRs • Contribute to peer projects

Slide 85

Slide 85 text

@skentphd Thanks! Stuart Kent Detroit Labs @skentphd