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. #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 中断可能な計算インスタンス
  2. #mixleap 中断/再開 launch {
 val event = async { getEvent(id)}

    .await()
 saveEvent(event)
 } !13  ⾃分は中断して別のcoroutineを起動して  結果を受け取ったら再開する
  3. #mixleap 中断/再開 launch {
 val localEvent = async { getEvent(id)}


    val remoteEvent = async { saveEvent(event)}
 localEvent.await() ?: remoteEvent.await().also {
 //save
 }
 } !14
  4. #mixleap 中断/再開 launch {
 val localEvent = async { getEvent(id)}


    val remoteEvent = async { saveEvent(event)}
 localEvent.await() ?: remoteEvent.await().also {
 //save
 }
 } !15
  5. #mixleap 中断/再開 launch {
 val localEvent = async { getEvent(id)}


    val remoteEvent = async { saveEvent(event)}
 localEvent.await() ?: remoteEvent.await().also {
 //save
 }
 } !16
  6. #mixleap 中断/再開 launch {
 val localEvent = async { getEvent(id)}


    val remoteEvent = async { saveEvent(event)}
 localEvent.await() ?: remoteEvent.await().also {
 //save
 }
 } !17 実⾏をキャンセル
  7. #mixleap 中断/再開 launch {
 val localEvent = async { getEvent(id)}


    val remoteEvent = async { saveEvent(event)}
 localEvent.await() ?: remoteEvent.await().also {
 //save
 }
 } !18 実⾏をキャンセル キャンセル??
  8. #mixleap 中断/再開 launch {
 val localEvent = async { getEvent(id)}


    val remoteEvent = async { saveEvent(event)}
 localEvent.await() ?: remoteEvent.await().also {
 //save
 }
 } !19 実⾏をキャンセル ⽌まらない!!
  9. #mixleap 中断/再開 launch {
 val localEvent = async { getEvent(id)}


    val remoteEvent = async { saveEvent(event)}
 localEvent.await() ?: remoteEvent.awaint().also {
 //save
 }
 } !20 Deprecated
  10. #mixleap 中断/再開 coroutineScope {
 val localEvent = async { getEvent(id)}


    val remoteEvent = async { saveEvent(event)}
 localEvent.await() ?: remoteEvent.await().also {
 //save
 }
 } !23
  11. #mixleap 中断/再開 coroutineScope {
 val localEvent = async { getEvent(id)}


    val remoteEvent = async { saveEvent(event)}
 localEvent.await() ?: remoteEvent.await().also {
 //save
 }
 } !24 実⾏をキャンセル
  12. #mixleap 中断/再開 coroutineScope {
 val localEvent = async { getEvent(id)}


    val remoteEvent = async { saveEvent(event)}
 localEvent.await() ?: remoteEvent.await().also {
 //save
 }
 } !25 実⾏をキャンセル キャンセルされる
  13. #mixleap 中断/再開 coroutineScope {
 val localEvent = async { getEvent(id)}


    val remoteEvent = async { saveEvent(event)}
 localEvent.await() ?: remoteEvent.await().also {
 //save
 }
 } !26 実⾏をキャンセル キャンセルされる
  14. #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 関数の振る舞いをコンパイラに伝える仕組み
  15. #mixleap Contracts !33 fun main(){
 val s: String? = ""


    if(!s.isNullOrEmpty()) {
 s.first()
 }
 }
  16. #mixleap Contracts !34 fun main(){
 val s: String? = ""


    if(!s.isNullOrEmpty()) {
 s.first() //ここでコンパイルエラー
 }
 }
  17. #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での実装
  18. #mixleap Contracts !36 public inline fun CharSequence?.isNullOrEmpty(): Boolean{
 contract{
 returns(false)

    implies (this@isNullOrEmpty != null)
 }
 return this == null || this.length == 0
 } コンパイラにfalse時のNullじゃない事を教えてる
  19. #mixleap Contracts !37 fun main(){
 val s: String? = ""


    if(!s.isNullOrEmpty()) {
 s.first() // 実⾏可能
 }
 }
  20. #mixleap inline class • インライン展開されるクラス • Primary constructorにread only なプロパティを持つ

    • Backing fieldを持つプロパティは定義できない • ===で⽐較不可能 • initialization blockが持てない • SuccessOrFailure Resultで使われている !39
  21. #mixleap inline class inline class Hours(val value: Int) {
 fun

    toMinutes() = value * 60
 } fun main() {
 val hour = Hours(3)
 println(hour.toMinutes()) 
 } !40
  22. #mixleap inline class // 現時点でenumはinline化できない
 inline enum class Unit {


    SECONDS_PER_MINUTE(60),
 MINUTES_PER_HOUR(60),
 HOURS_PER_DAY(24)
 } !41
  23. #mixleap 細かなUpdate • whenの()内で変数宣⾔が可能に • annotation classがネストして宣⾔可能に • 符号なし整数型が追加 •

    Collection, Map Array, SequenceにifEmptyが追加 • main()が引数がオプションに変更 • and more !43
  24. #mixleap まとめ • Coroutineがやっとstableに • Experimentalの活⽤は慎重に… • Kotlinらしいコードが書きやすくなった • 今年〜来年にかけて1.3対応したライブラリがふえるのでは

    • Contracts等でORM等のNullSafeじゃなかったところも変わるかもしれない • とりあえずv1.3へ上げて楽しくKotlin書きましょう! !45