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

Convert Java File to Kotlin File おかしな変換集

Convert Java File to Kotlin File おかしな変換集

Kotlin Fest 2018のLT大会発表のスライドです。

Takehata Naoto

August 25, 2018

Transcript

20 slides
  1. public String getMessage() { UserService userService = new UserService(); return

    userService.createMessage(); } val message: String get() { val userService = UserService() return userService.createMessage() } なんかプロパティになってる Java Kotlin
  2. 拡張プロパティ val score1 = 0 val score2 = 0 val

    totalScore get() = score1 + score2 ・get〜という名前の引数なしメソッドが  全てプロパティとして認識されてしまう ・割とゴリゴリ処理を書いた拡張プロパティが  できあがった
  3. Java Kotlin public sta8c Integer calc(Integer num1, Integer num2) {

    return num1 + num2; } companion object { fun calc(num1: Int?, num2: Int?): Int { return num1!! + num2!! } } 謎のオブジェクトcompanionが出現する
  4. コンパニオンオブジェクト Javaから呼び出そうとすると、Companionというオブジェクトを 経由しなくてはならなくなる companion object { fun execute() { //

    ・・・
 } } Sample.Companion.execute(); Java Kotlin staticな関数を定義したい時、コンパニオンオブジェクトを使う
  5. Java public String execute(Integer userId, Integer targetId) { Map<Integer, String>

    userMap = createUserMap(); return userMap.entrySet() .stream() .filter(entry -> { if (userMap.containsKey(userId)) { return true; } if (userMap.containsKey(targetId)) { return true; } return false; }).map(entry -> { return entry.getValue(); }).toString(); }
  6. Kotlin fun execute(userId: Int?, targetId: Int?): String { val userMap

    = createUserMap() return userMap.entries .stream() .filter { entry -> if (userMap.containsKey(userId)) { [email protected]() .stream() .filter true } if (userMap.containsKey(targetId)) { [email protected]() .stream() .filter true } false }.map { entry -> entry.value }.toString() }

More Decks by Takehata Naoto

Other Decks in Programming