Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin v1.3 Features

BulBulPaul
November 01, 2018

Kotlin v1.3 Features

MixLeap Study -Kotlin Conf 2018 Recap-での発表資料

BulBulPaul

November 01, 2018
Tweet

More Decks by BulBulPaul

Other Decks in Technology

Transcript

  1. Kotlin v1.3 

    Update Features
    2018-11-01 MixLeap - Kotlin Conf 2018 Recap -
    @bulbulpaul

    View full-size slide

  2. About me
    !2
    おかだ のぶお
    ポール@bulbulpaul
    Yahoo!カレンダー
    Backend Engineer
    Python, Kotlin, Rust
    name:
    :
    service:
    job:
    lang:

    View full-size slide

  3. #mixleap
    Kotlin v1.3 Features
    • Coroutine
    • Experimental Features
    • Kotlin Native Beta
    • Other Update
    !5

    View full-size slide

  4. #mixleap
    Kotlin v1.3 Features
    • Coroutine
    • Experimental Features
    • Kotlin Native Beta
    • Other Update
    !6

    View full-size slide

  5. #mixleap
    Coroutine
    Import kotlin.coroutines.experimental.
    !8
    *

    View full-size slide

  6. #mixleap
    Coroutine
    Import kotlin.coroutines.experimental.
    !9
    *

    View full-size slide

  7. #mixleap
    What’s Coroutine
    A coroutine can be thought of as an instance of
    suspendable computation, i.e. the one that can suspend
    at some points and later resume execution possibly on
    another thread. Coroutines calling each other (and passing
    data back and forth) can form the machinery for
    cooperative multitasking.
    !10
    Ҿ༻: https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md
    中断可能な計算インスタンス

    View full-size slide

  8. #mixleap
    What’s Coroutine
    • 中断可能な計算インスタンス

    → ざっくり⾔うと軽量なスレッド
    • 特徴のスレッドに依存しない
    • 昨年のKotlinConfで「実質Stableだよ」発⾔
    !11

    View full-size slide

  9. #mixleap
    What’s Coroutine
    • 中断可能な計算インスタンス

    → ざっくり⾔うと軽量なスレッド
    • 特徴のスレッドに依存しない
    • 昨年のKotlinConfで「実質Stableだよ」発⾔

    → まさかの仕様変更!!
    !12

    View full-size slide

  10. #mixleap
    中断/再開
    launch {

    val event = async { getEvent(id)} .await()

    saveEvent(event)

    }
    !13
     ⾃分は中断して別のcoroutineを起動して
     結果を受け取ったら再開する

    View full-size slide

  11. #mixleap
    中断/再開
    launch {

    val localEvent = async { getEvent(id)}

    val remoteEvent = async { saveEvent(event)}

    localEvent.await() ?: remoteEvent.await().also {

    //save

    }

    }
    !14

    View full-size slide

  12. #mixleap
    中断/再開
    launch {

    val localEvent = async { getEvent(id)}

    val remoteEvent = async { saveEvent(event)}

    localEvent.await() ?: remoteEvent.await().also {

    //save

    }

    }
    !15

    View full-size slide

  13. #mixleap
    中断/再開
    launch {

    val localEvent = async { getEvent(id)}

    val remoteEvent = async { saveEvent(event)}

    localEvent.await() ?: remoteEvent.await().also {

    //save

    }

    }
    !16

    View full-size slide

  14. #mixleap
    中断/再開
    launch {

    val localEvent = async { getEvent(id)}

    val remoteEvent = async { saveEvent(event)}

    localEvent.await() ?: remoteEvent.await().also {

    //save

    }

    }
    !17
    実⾏をキャンセル

    View full-size slide

  15. #mixleap
    中断/再開
    launch {

    val localEvent = async { getEvent(id)}

    val remoteEvent = async { saveEvent(event)}

    localEvent.await() ?: remoteEvent.await().also {

    //save

    }

    }
    !18
    実⾏をキャンセル
    キャンセル??

    View full-size slide

  16. #mixleap
    中断/再開
    launch {

    val localEvent = async { getEvent(id)}

    val remoteEvent = async { saveEvent(event)}

    localEvent.await() ?: remoteEvent.await().also {

    //save

    }

    }
    !19
    実⾏をキャンセル
    ⽌まらない!!

    View full-size slide

  17. #mixleap
    中断/再開
    launch {

    val localEvent = async { getEvent(id)}

    val remoteEvent = async { saveEvent(event)}

    localEvent.await() ?: remoteEvent.awaint().also {

    //save

    }

    }
    !20
    Deprecated

    View full-size slide

  18. Coroutine Scope

    View full-size slide

  19. #mixleap
    Coroutine Scope
    • コルーチンのスコープを定義する
    • ⾃動的にコンテキスト要素とキャンセルの両⽅を

    伝播させる事が可能
    !22

    View full-size slide

  20. #mixleap
    中断/再開
    coroutineScope {

    val localEvent = async { getEvent(id)}

    val remoteEvent = async { saveEvent(event)}

    localEvent.await() ?: remoteEvent.await().also {

    //save

    }

    }
    !23

    View full-size slide

  21. #mixleap
    中断/再開
    coroutineScope {

    val localEvent = async { getEvent(id)}

    val remoteEvent = async { saveEvent(event)}

    localEvent.await() ?: remoteEvent.await().also {

    //save

    }

    }
    !24
    実⾏をキャンセル

    View full-size slide

  22. #mixleap
    中断/再開
    coroutineScope {

    val localEvent = async { getEvent(id)}

    val remoteEvent = async { saveEvent(event)}

    localEvent.await() ?: remoteEvent.await().also {

    //save

    }

    }
    !25
    実⾏をキャンセル
    キャンセルされる

    View full-size slide

  23. #mixleap
    中断/再開
    coroutineScope {

    val localEvent = async { getEvent(id)}

    val remoteEvent = async { saveEvent(event)}

    localEvent.await() ?: remoteEvent.await().also {

    //save

    }

    }
    !26
    実⾏をキャンセル
    キャンセルされる

    View full-size slide

  24. #mixleap
    もっと知りたい⽅は…
    • Kotlin Festの⼋⽊さんのセッションは⾮常にわかりやすい
    • https://speakerdeck.com/sys1yagi/kotlin-korutinwo-li-
    jie-siyou
    !27

    View full-size slide

  25. Experimental Features

    View full-size slide

  26. #mixleap
    Experimental
    • Contracts
    • Inline class
    • Experimental API
    !29

    View full-size slide

  27. #mixleap
    Experimental
    • Contracts
    • Inline class
    • Experimental API
    !30

    View full-size slide

  28. #mixleap
    What’s Contracts??
    Support a way to explicitly express some aspects of
    function's behavior, thus allowing programmers to
    cooperate with Kotlin compiler by providing it with
    additional guarantees, getting more complete and intense
    analysis in return.
    !31
    関数の振る舞いをコンパイラに伝える仕組み

    View full-size slide

  29. #mixleap
    Contracts
    • Calls In Place

    →この処理は「⼀回実⾏される」「⼀回以上実⾏される」
    等の情報をコンパイラへ伝える
    • Returns

    →返り値の戻り条件等をコンパイラへ伝える
    !32

    View full-size slide

  30. #mixleap
    Contracts
    !33
    fun main(){

    val s: String? = ""

    if(!s.isNullOrEmpty()) {

    s.first()

    }

    }

    View full-size slide

  31. #mixleap
    Contracts
    !34
    fun main(){

    val s: String? = ""

    if(!s.isNullOrEmpty()) {

    s.first() //ここでコンパイルエラー

    }

    }

    View full-size slide

  32. #mixleap
    Contracts
    !35
    public inline fun CharSequence?.isNullOrEmpty(): Boolean{

    contract{

    returns(false) implies (this@isNullOrEmpty != null)

    }

    return this == null || this.length == 0

    }
    Kotlin v1.3での実装

    View full-size slide

  33. #mixleap
    Contracts
    !36
    public inline fun CharSequence?.isNullOrEmpty(): Boolean{

    contract{

    returns(false) implies (this@isNullOrEmpty != null)

    }

    return this == null || this.length == 0

    }
    コンパイラにfalse時のNullじゃない事を教えてる

    View full-size slide

  34. #mixleap
    Contracts
    !37
    fun main(){

    val s: String? = ""

    if(!s.isNullOrEmpty()) {

    s.first() // 実⾏可能

    }

    }

    View full-size slide

  35. Inline class

    View full-size slide

  36. #mixleap
    inline class
    • インライン展開されるクラス
    • Primary constructorにread only なプロパティを持つ
    • Backing fieldを持つプロパティは定義できない
    • ===で⽐較不可能
    • initialization blockが持てない
    • SuccessOrFailure Resultで使われている
    !39

    View full-size slide

  37. #mixleap
    inline class
    inline class Hours(val value: Int) {

    fun toMinutes() = value * 60

    }
    fun main() {

    val hour = Hours(3)

    println(hour.toMinutes()) 

    }
    !40

    View full-size slide

  38. #mixleap
    inline class
    // 現時点でenumはinline化できない

    inline enum class Unit {

    SECONDS_PER_MINUTE(60),

    MINUTES_PER_HOUR(60),

    HOURS_PER_DAY(24)

    }
    !41

    View full-size slide

  39. #mixleap
    細かなUpdate
    • whenの()内で変数宣⾔が可能に
    • annotation classがネストして宣⾔可能に
    • 符号なし整数型が追加
    • Collection, Map Array, SequenceにifEmptyが追加
    • main()が引数がオプションに変更
    • and more
    !43

    View full-size slide

  40. #mixleap
    まとめ
    • Coroutineがやっとstableに
    • Experimentalの活⽤は慎重に…
    • Kotlinらしいコードが書きやすくなった
    • 今年〜来年にかけて1.3対応したライブラリがふえるのでは
    • Contracts等でORM等のNullSafeじゃなかったところも変わるかもしれない
    • とりあえずv1.3へ上げて楽しくKotlin書きましょう!
    !45

    View full-size slide

  41. Have a nice Kotlin!

    View full-size slide