$30 off During Our Annual Pro Sale. View Details »

Swift と Kotlin

Yuka Ezura
September 16, 2017

Swift と Kotlin

iosdc2017

Yuka Ezura

September 16, 2017
Tweet

More Decks by Yuka Ezura

Other Decks in Programming

Transcript

  1. Swift & Kotlin
    2017/9/16
    ezura

    View Slide

  2. ezura (@eduraaa)
    • iOS engineer @ LINE

    • like: programming
    • mock mock radio
    • ΪϦγϟ఩ֶ / Swift
    • Blending Kotlin's culture into Swift
    • ఈ͔ΒֶͿ Swift
    • …

    View Slide

  3. official on Android

    View Slide

  4. Kotlin

    View Slide

  5. Kotlin
    OOP
    extension
    interface

    default
    implementati
    on
    generics
    delegation
    null
    safety
    tailrec
    static

    type
    Java
    GC
    sealed
    class
    variance

    declaration
    object
    JS
    companion 

    object
    if, when
    expression
    Type
    inference
    (Corout
    ines)
    inline
    annotation
    FP

    View Slide

  6. • Started in 2010
    • Object-oriented programming
    • Support functional programming
    • Static typing
    • Type inference
    • Null safety
    • Generic programming

    View Slide

  7. Kotlin’s background

    View Slide

  8. started in 2010
    first official 1.0 release in 2016
    Let's make a new language

    based on the knowledge 

    that has developed IDEs

    View Slide

  9. class Class: Super() {
    var property = ""
    fun method() {
    var nullable: String? = null
    if (nullable != null) {
    nullable.length
    }
    val list = listOf(1, 2, 3)
    list.forEach { print(it) }
    }
    }

    View Slide

  10. class Class: Super() {
    var property = ""
    fun method() {
    var nullable: String? = null
    if (nullable != null) {
    nullable.length
    }
    val list = listOf(1, 2, 3)
    list.forEach { print(it) }
    }
    }
    Object-oriented programming

    View Slide

  11. class Class: Super() {
    var property = ""
    fun method() {
    var nullable: String? = null
    if (nullable != null) {
    nullable.length
    }
    val list = listOf(1, 2, 3)
    list.forEach { print(it) }
    }
    } Support functional programming

    View Slide

  12. class Class: Super() {
    var property = ""
    fun method() {
    var nullable: String? = null
    if (nullable != null) {
    nullable.length
    }
    val list = listOf(1, 2, 3)
    list.forEach { print(it) }
    }
    }
    Static typing

    View Slide

  13. class Class: Super() {
    var property = ""
    fun method() {
    var nullable: String? = null
    if (nullable != null) {
    nullable.length
    }
    val list = listOf(1, 2, 3)
    list.forEach { print(it) }
    }
    }
    Type inference

    View Slide

  14. class Class: Super() {
    var property = ""
    fun method() {
    var nullable: String? = null
    if (nullable != null) {
    nullable.length
    }
    val list = listOf(1, 2, 3)
    list.forEach { print(it) }
    }
    }
    Generic programming

    View Slide

  15. class Class: Super() {
    var property = ""
    fun method() {
    var nullable: String? = null
    if (nullable != null) {
    nullable.length
    }
    val list = listOf(1, 2, 3)
    list.forEach { print(it) }
    }
    }
    Null safety

    View Slide

  16. • Started in 2010
    • Object-oriented programming
    • Support functional programming
    • Static typing
    • Type inference
    • Null safety
    • Generic programming

    View Slide

  17. • Started in 2010
    • Object-oriented programming
    • Support functional programming
    • Static typing
    • Type inference
    • Null safety
    • Generic programming

    View Slide

  18. Swift ͱ Kotlin ͸ࣅ͍ͯΔʁʁ

    View Slide

  19. Swift ͱ Kotlin ͸ࣅ͍ͯΔʁʁ

    View Slide

  20. Swift ͱ Kotlin ͸ࣅ͍ͯΔʁʁ
    ❌ runtime environment
    ❌ memory management
    ❌ background
    ❌ calling Java/Objective-C
    ❌ Culture
    ❌ features

    View Slide

  21. Swift ͱ Kotlin ͸ࣅ͍ͯΔʁʁ
    Is the learning cost low?

    View Slide

  22. Swift ͱ Kotlin ͸ࣅ͍ͯΔʁʁ
    ෦෼΋͋Δ
    Is the learning cost low?

    View Slide

  23. Swift ͱ Kotlin ͸ࣅ͍ͯΔʁʁ
    ෦෼΋͋Δ
    Is the learning cost low?

    View Slide

  24. Kotlin ֶश

    Swift ஌ࣝͰϒʔετՄೳ

    View Slide

  25. Index
    • What is Kotlin
    • Is Kotlin like Swift?
    • some keyword
    • some features
    • Traps

    View Slide

  26. Index
    • What is Kotlin
    • Is Kotlin like Swift?
    • some keyword
    • some features
    • Traps

    View Slide

  27. Syntax
    Swift Kotlin
    var, let
    func f(arg: Any) -> Any
    class AnyType { … }
    var obj = Obj()
    obj.method(arg: v)
    var, val
    fun f(arg: Any): Any
    class AnyType { … }
    var obj = Obj()
    obj.method(v)
    see also: http://nilhcem.com/swift-is-like-kotlin/

    View Slide

  28. Support functional programming
    Swift Kotlin
    val f = { x: Int ->
    /* … */
    }
    obj.map { /* … */ }
    let f = { Int -> String in
    /* … */
    }
    obj.map { /* … */ }

    View Slide

  29. Support functional programming
    Swift Kotlin
    val f = { x: Int ->
    /* … */
    }
    obj.map { /* … */ }
    let f = { Int -> String in
    /* … */
    }
    obj.map { /* … */ }
    constant constant

    View Slide

  30. Support functional programming
    Swift Kotlin
    val f = { x: Int ->
    /* … */
    }
    obj.map { /* … */ }
    let f = { Int -> String in
    /* … */
    }
    obj.map { /* … */ }
    first class function first class function

    View Slide

  31. Support functional programming
    Swift Kotlin
    val f = { x: Int ->
    /* … */
    }
    obj.map { /* … */ }
    let f = { Int -> String in
    /* … */
    }
    obj.map { /* … */ }
    higher-order function higher-order function

    View Slide

  32. Null safety
    Swift Kotlin
    let i: Int?
    i?.description
    if let i = i {
    i.description
    }
    val i: Int?
    i?.toString()
    if (i != null) {
    i.toString()
    }

    View Slide

  33. Null safety
    Swift Kotlin
    let i: Int?
    i?.description
    if let i = i {
    i.description
    }
    val i: Int?
    i?.toString()
    if (i != null) {
    i.toString()
    }

    View Slide

  34. extension
    Swift Kotlin
    extension String {
    func f() {}
    var v: String { return "" }
    }
    let string = ""
    string.f()
    string.v
    fun String.f() = {}
    val String.v: String get() = ""

    val string = ""
    string.f()
    string.v

    View Slide

  35. default implementation 

    for protocol/interface
    Swift Kotlin
    protocol P {
    func f()
    }
    extension P {
    func f() {
    /* do something */
    }
    }
    interface I {
    fun f() {
    /* do something */
    }
    }

    View Slide

  36. • Started in 2010
    • Object-oriented programming
    • Support functional programming
    • Static typing
    • Type inference
    • Null safety
    • Generic programming
    • Some syntax
    • Extension
    • Default implementation

    View Slide

  37. Kotlin
    OOP
    extension
    interface

    default
    implementati
    on
    generics
    delegation
    null
    safety
    tailrec
    static

    type
    Java
    GC
    sealed
    class
    variance

    declaration
    object
    JS
    companion 

    object
    if, when
    expression
    Type
    inference
    (Corout
    ines)
    inline
    annotation
    FP

    View Slide

  38. Kotlin
    OOP
    extension
    interface

    default
    implementati
    on
    generics
    delegation
    null
    safety
    tailrec
    static

    type
    Java
    GC
    sealed
    class
    variance

    declaration
    object
    JS
    companion 

    object
    if, when
    expression
    Type
    inference
    (Corout
    ines)
    inline
    annotation
    FP

    View Slide

  39. Trap

    View Slide

  40. extension
    Swift Kotlin
    extension String {
    func f() {}
    var v: String { return "" }
    }

    fun String.f() = {}
    val String.v: String get() = “"

    View Slide

  41. extension
    Type Type
    function
    property
    function
    property
    Swift Kotlin

    View Slide

  42. extension
    Type Type
    function
    property
    function
    property
    Swift Kotlin

    View Slide

  43. extension
    function
    Util class
    instance
    static Result method(Some target)
    Some

    Type
    Java

    View Slide

  44. extension
    Util.method(instance)
    Java
    function
    Util class
    instance
    Some

    Type
    static Result method(Some target)

    View Slide

  45. extension
    Collections.max(list)
    Java
    function
    Util class
    instance
    Some

    Type
    static Result method(Some target)

    View Slide

  46. extension
    max(list)
    Java
    function
    Util class
    instance
    Some

    Type
    static Result method(Some target)

    View Slide

  47. extension
    max(list)
    Java
    function
    Util class
    instance
    Some

    Type
    list.max()
    want to …
    static Result method(Some target)

    View Slide

  48. extension
    Some

    Type
    function
    property
    Kotlin
    Util.extensionMethod(instance)

    View Slide

  49. extension
    Some

    Type
    function
    property
    Kotlin
    obj.extensionMethod()

    View Slide

  50. extension
    Type Type
    function
    property
    function
    property
    Swift Kotlin
    extend type by

    functions/properties
    add

    functions/properties

    View Slide

  51. extension
    Type Type
    function
    property
    function
    property
    Swift Kotlin
    protocol interface
    extension String: MyProtocol {
    /* ... */
    }
    -

    View Slide

  52. extension
    Type Type
    function
    property
    function
    property
    Swift Kotlin
    extend type by

    functions/properties
    add

    functions/properties

    View Slide

  53. default implementation 

    for protocol/interface
    Swift Kotlin
    protocol P {
    func f()
    }
    extension P {
    func f() {
    /* do something */
    }
    }
    interface I {
    fun f() {
    /* do something */
    }
    }

    View Slide

  54. default implementation 

    for protocol/interface
    Swift Kotlin
    protocol P {
    func f()
    }
    extension P {
    func f() {
    /* do something */
    }
    }
    interface I {
    fun f() {
    /* do something */
    }
    }
    P f()
    extension P
    f()

    View Slide

  55. default implementation 

    for protocol/interface
    Swift Kotlin
    protocol P {
    func f()
    }
    extension P {
    func f() {
    /* do something */
    }
    }
    interface I {
    fun f()
    }
    fun I.f() {
    /* do something */
    }
    P f()
    extension extension
    P f()

    View Slide

  56. default implementation 

    for protocol/interface
    Swift Kotlin
    protocol P {
    func f()
    }
    extension P {
    func f() {
    /* do something */
    }
    }
    interface I {
    fun f()
    }
    fun I.f() {
    /* do something */
    }
    class A: I {} // error
    P f()
    extension extension
    P f()

    View Slide

  57. default implementation 

    for protocol/interface
    Swift Kotlin
    protocol P {
    func f()
    }
    extension P where … {
    func f() {
    /* do something */
    }
    }
    interface I {
    fun f() {
    /* do something */
    }
    }
    P f()
    extension

    where …
    P
    f()

    View Slide

  58. default implementation 

    for protocol/interface
    Protocol
    Interface
    Implementation
    Implementation
    extension
    Swift Kotlin

    View Slide

  59. Null safety
    Swift Kotlin
    let i: Int?
    i?.description
    if let i = i {
    i.description
    }
    val i: Int?
    i?.toString()
    if (i != null) {
    i.toString()
    }

    View Slide

  60. Null safety
    Swift Kotlin
    let i: Int?
    i?.description
    if let i = i {
    i.description
    }
    val i: Int?
    i?.toString()
    if (i != null) {
    i.toString()
    }
    Optional Nullable

    View Slide

  61. Null safety
    Swift Kotlin
    enum Optional {
    case none
    case some(Wrapped)
    }
    nullable
    Optional Nullable

    View Slide

  62. Null safety
    Swift Kotlin
    enum Optional {
    case none
    case some(Wrapped)
    }
    nullable
    exist some value 

    or not null or not null
    Optional Nullable

    View Slide

  63. Null safety
    Swift Kotlin
    ɹ
    exist some value 

    or not not null or null
    Value none
    or Value or null
    Optional Nullable

    View Slide

  64. Null safety
    Swift Kotlin
    ɹ
    exist some value 

    or not not null or null
    Value none
    or Value or null
    Optional Nullable
    Value = Int?

    View Slide

  65. Null safety
    Swift Kotlin
    ɹ
    exist some value 

    or not not null or null
    Int? none
    or Int? or null
    Optional Nullable
    Value = Int?

    View Slide

  66. Null safety
    Swift Kotlin
    ɹ
    exist some value 

    or not not null or null
    1 

    or

    nil
    none
    or 1 

    or

    null
    or null
    Optional Nullable
    Value = Int?

    View Slide

  67. Null safety
    Swift Kotlin
    ɹ
    exist some value 

    or not not null or null
    .some

    (nil) none
    or or null
    null
    ??
    Optional Nullable
    Value = Int?

    View Slide

  68. Some familiar functions
    Swift Kotlin
    [1, 2, 3].forEach {
    if $0 == 2 { return }
    print($0)
    }
    listOf(1, 2, 3).forEach {
    if (it == 2) { return }
    println(it)
    }

    View Slide

  69. Some familiar functions
    Swift Kotlin
    [1, 2, 3].forEach {
    if $0 == 2 { return }
    print($0)
    }
    listOf(1, 2, 3).forEach {
    if (it == 2) { return }
    println(it)
    }

    View Slide

  70. [1, 2, 3].forEach {
    if $0 == 2 { return }
    print($0)
    }
    listOf(1, 2, 3).forEach {
    if (it == 2) { return }
    println(it)
    }
    Some familiar functions
    Swift Kotlin
    1
    3
    1

    View Slide

  71. [1, 2, 3].forEach {
    if $0 == 2 { return }
    print($0)
    }
    listOf(1, 2, 3).forEach {
    if (it == 2) { return }
    println(it)
    }
    Some familiar functions
    Swift Kotlin
    1
    3
    1

    View Slide

  72. Some familiar functions
    Swift Kotlin
    @_inlineable
    public func forEach(
    _ body: (Element) throws -> Void
    ) rethrows {
    for element in self {
    try body(element)
    }
    }
    public inline fun
    Iterable.forEach(action: (T) -> Unit): Unit {
    for (element in this) action(element)
    }

    View Slide

  73. Some familiar functions
    Swift Kotlin
    @_inlineable
    public func forEach(
    _ body: (Element) throws -> Void
    ) rethrows {
    for element in self {
    try body(element)
    }
    }
    public inline fun
    Iterable.forEach(action: (T) -> Unit): Unit {
    for (element in this) action(element)
    }

    View Slide

  74. Some familiar functions
    Swift Kotlin
    @_inlineable
    public func forEach(
    _ body: (Element) throws -> Void
    ) rethrows {
    for element in self {
    try body(element)
    }
    }
    public inline fun
    Iterable.forEach(action: (T) -> Unit): Unit {
    for (element in this) action(element)
    }
    inline: ݺͼग़͠ݩʹத਎Λల։
    inline: ݺͼग़͠ݩʹத਎Λల։

    View Slide

  75. Some familiar functions
    Swift Kotlin
    @_inlineable
    public func forEach(
    _ body: (Element) throws -> Void
    ) rethrows {
    for element in self {
    try body(element)
    }
    }
    public inline fun
    Iterable.forEach(action: (T) -> Unit): Unit {
    for (element in this) action(element)
    }
    inline: ϩδοΫʹӨڹ͠ͳ͍
    inline: ϩδοΫʹӨڹ͢Δ

    View Slide

  76. Some familiar functions
    Swift Kotlin
    @_inlineable
    public func forEach(
    _ body: (Element) throws -> Void
    ) rethrows {
    for element in self {
    try body(element)
    }
    }
    public inline fun
    Iterable.forEach(action: (T) -> Unit): Unit {
    for (element in this) action(element)
    }
    {
    if (it == 2) { return }
    println(it)
    }
    inline: ݺͼग़͠ݩʹத਎Λల։

    View Slide

  77. Some familiar functions
    Kotlin
    listOf(1, 2, 3).forEach {
    if (it == 2) { return }
    println(it)
    }
    Iterable $receiver$iv =
    (Iterable)CollectionsKt.listOf(new Integer[]
    {Integer.valueOf(1), Integer.valueOf(2),
    Integer.valueOf(3)});
    Iterator var2 = $receiver$iv.iterator();
    while(var2.hasNext()) {
    Object element$iv = var2.next();
    int it = ((Number)element$iv).intValue();
    if(it == 2) {
    return;
    }
    System.out.println(it);
    }

    View Slide

  78. Some familiar functions
    Kotlin
    listOf(1, 2, 3).forEach {
    if (it == 2) { return }
    println(it)
    }
    /* ... */
    while(var2.hasNext()) {
    Object element$iv = var2.next();
    int it = ((Number)element$iv).intValue();
    if(it == 2) {
    return;
    }
    System.out.println(it);
    }

    View Slide

  79. listOf(1, 2, 3).forEach {
    if (it == 2) { return }
    println(it)
    }
    /* ... */
    while(var2.hasNext()) {
    Object element$iv = var2.next();
    int it = ((Number)element$iv).intValue();
    if(it == 2) {
    return;
    }
    System.out.println(it);
    }
    Some familiar functions
    Kotlin

    View Slide

  80. listOf(1, 2, 3).forEach {
    if (it == 2) { return }
    println(it)
    }
    /* ... */
    while(var2.hasNext()) {
    Object element$iv = var2.next();
    int it = ((Number)element$iv).intValue();
    if(it == 2) {
    return;
    }
    System.out.println(it);
    }
    Some familiar functions
    Kotlin
    non-local return

    View Slide

  81. [1, 2, 3].forEach {
    if $0 == 2 { return }
    print($0)
    }
    listOf(1, 2, 3).forEach {
    if (it == 2) { return }
    println(it)
    }
    Some familiar functions
    Swift Kotlin
    1
    3
    1

    View Slide

  82. Kotlin
    OOP
    extension
    interface

    default
    implementati
    on
    generics
    delegation
    null
    safety
    tailrec
    static

    type
    Java
    GC
    sealed
    class
    variance

    declaration
    object
    JS
    companion 

    object
    if, when
    expression
    Type
    inference
    (Corout
    ines)
    inline
    annotation
    FP

    View Slide

  83. Conclusion

    View Slide

  84. • Started in 2010
    • Object-oriented programming
    • Support functional programming
    • Static typing
    • Type inference
    • Null safety
    • Generic programming
    • Some syntax
    • Extension
    • Default implementation

    View Slide

  85. &

    View Slide

  86. Same word, different concept

    View Slide

  87. Swift ͱ Kotlin
    ͲͪΒ΋ɺଞͷݴޠ͔Βػೳ΍ࢥ૝ΛऔΓೖΕ͍ͯΔ
    ͦΕΛͦΕͧΕʹݟ߹ͬͨܗͰ༥߹ͤͭͭ͞΋ɺ

    ֤ࣗͷจԽΛங͍͍ͯΔ
    ڞ௨͢Δ෦෼͸ੜ͡Δ

    View Slide

  88. enjoy Swift, enjoy Kotlin :)

    View Slide

  89. References
    Kotlin (official reference)

    https://kotlinlang.org/docs/reference/
    The Swift Programming Language (Swift 4)

    https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AboutTheLanguageReference.html
    Programming Kotlin

    by Stefan Bocutiu, Stephen Samuel
    Swift is like Kotlin: Hacker News (discussion)

    https://news.ycombinator.com/item?id=14364612
    Swift is like Kotlin

    http://nilhcem.com/swift-is-like-kotlin/
    SwiftKotlin

    https://github.com/angelolloqui/SwiftKotlin
    Swift vs Kotlin for real iOS/Android apps (ͱͯ΋ྑ͍هࣄͰͨ͠ʂ)

    http://angelolloqui.com/blog/38-Swift-vs-Kotlin-for-real-iOS-Android-apps
    Swift is NOT like Kotlin..?

    http://blog.applibot.co.jp/blog/2017/04/03/swift-is-not-like-kotlin/
    iOSΞϓϦ։ൃऀ͔ΒݟͨKotlin (࣌ؒͷ౎߹Ͱল͍ͨ෦෼΋͋Γ·͕͢ɺߏจͷൺֱ͕៉ྷʹ·ͱ·͍ͬͯ·͢)

    https://speakerdeck.com/koishi/iosapurikai-fa-zhe-karajian-takotlin
    null҆શͰͳ͍ݴޠ͸ɺ΋͸΍ϨΨγʔݴޠͩ (nullable ͷछྨͱಛ௃)

    http://qiita.com/koher/items/e4835bd429b88809ab33#nullable-%E3%81%AE%E7%A8%AE%E9%A1%9E%E3%81%A8%E7%89%B9%E5%BE%B4
    Android։ൃΛड஫͔ͨ͠ΒKotlinΛΨοπϦ࢖ͬͯΈͨΒ࠷ߴͩͬͨ

    http://qiita.com/omochimetaru/items/98e015b0b694dd97f323
    ๩͍͠SwiftΤϯδχΞͷͨΊͷKotlinೖ໳

    https://speakerdeck.com/inamiy/mang-siiswiftenziniafalsetamefalsekotlinru-men
    Kotlin and Swift. Is it a whole new era in Mobile Development?

    https://medium.com/@andycherkashyn/kotlin-and-swift-is-it-a-new-mobile-development-era-b4c5e81feb08
    Swift/Kotlin߹ಉษڧձ

    https://oi-study.connpass.com/event/62521/

    View Slide

  90. Q & A

    View Slide

  91. [1, 2, 3].forEach {
    if $0 == 2 { return }
    print($0)
    }
    listOf(1, 2, 3).forEach {
    if (it == 2) { return }
    println(it)
    }
    Some familiar functions
    Swift Kotlin
    1
    3
    1
    ࣭໰
    Swift Έ͍ͨͳڍಈʹͰ͖Δʁ

    View Slide

  92. A: ಗ໊ؔ਺Λ࢖͍·͢
    listOf(1, 2, 3).forEach(fun(element: Int) {
    if (element == 2) { return }
    print(element)
    return
    }) ͜ͷ return ͸ಗ໊ؔ਺ͷ return
    ͘͢͝ৄ͍͠આ໌

    JavaϓϩάϥϚ͕KotlinͰͭ·͖͕ͮͪͳͱ͜Ζ

    http://qiita.com/koher/items/
    d9411a00986f14683a3f#inline%2d%e3%81%a8%2dnon%2dlocal%2dreturn

    View Slide

  93. ձ৔Ͱ౴͑ͯͩͬͨ͘͞ @omochimetaru ͞Μɺ

    هࣄΛࣥචɺπΠʔτ͍͖ͨͩ·ͨ͠ @koher ͞Μ

    ͋Γ͕ͱ͏͍͟͝·͢ʂ

    View Slide

  94. Some familiar functions
    Swift
    @_inlineable
    public func first(
    where predicate: (Element) throws -> Bool
    ) rethrows -> Element? {
    var foundElement: Element?
    do {
    try self.forEach {
    if try predicate($0) {
    foundElement = $0
    throw _StopIteration.stop
    }
    }
    } catch is _StopIteration { }
    return foundElement
    }
    ྫ֎౤͛ͯ୤ग़
    ༨ஊ

    Swift Ͱ forEachൈ͚͍ͨͱ͖

    ྫ: first(where: )

    View Slide

  95. ࣭໰

    inline ࢦఆΛ͢Δ͜ͱʹΑΔϩδο
    ΫޡΓͷճආΛͲ͏͢Δ͔
    ճ౴͸དྷिதʹΞοϓσʔτ͠·͢ʂ
    ><

    View Slide