Slide 1

Slide 1 text

What a difference! <-Scala & Kotlin-> @kaelaela

Slide 2

Slide 2 text

kaelaela(まえかわ ゆういち) Backend engineer at アルプ株式会社/Alp, Inc Work: Scala with DDD with Eff Twitter: _kaelaela GitHub: kaelaela Scala: helf a year Kotlin: 2.5 years

Slide 3

Slide 3 text

Scala == Kotlin ??? ScalaとKotlinは同じ?

Slide 4

Slide 4 text

Kotlin extends Scala ??? KotlinはScalaを真似して作られた?

Slide 5

Slide 5 text

Scala philosophy / Scalaの哲学 Scalable language スケーラブルな言語 Unifies object-oriented and functional programming オブジェクト指向と関数型の統合 Interoperates seamlessly with Java Javaとのシームレスな相互運用

Slide 6

Slide 6 text

Kotlin philosophy / Kotlinの哲学 健全性よりも

Slide 7

Slide 7 text

Scala != Kotlin ??? ScalaとKotlinは全然違う?

Slide 8

Slide 8 text

Scala and Kotlin are similar data class User( var name: String = "anonymous" ) 似てる! case class User( name: String = "anonymous" )

Slide 9

Slide 9 text

Scala and Kotlin are VERY similar data class User... val user = User().apply { name = "yuichi" } println(user.name) // yuichi case class User... val user = User.apply( name = "yuichi" ) println(user.name) // yuichi めっちゃ似てる!

Slide 10

Slide 10 text

Scala case class User... val user = User.apply( name = "yuichi" ) println(user.name) // yuichi Tiny diff…? Kotlin data class User... val user = User().apply { name = "yuichi" } println(user.name) // yuichi チョット違う…?

Slide 11

Slide 11 text

case class User… object User { def apply(name: String): User = new User(name) def main(args: Array[String]) = { val user = User.apply(name = "yuichi") println(user.name) } } How is the class defined? data class User… fun main() { val user = User().apply {name = "yuichi"} println(user.name) } クラスはどう?

Slide 12

Slide 12 text

case class User… object User { def apply(name: String): User = new User(name) def main(args: Array[String]) = { val user = User.apply(name = "yuichi") println(user.name) } } data class User… fun main() { val user = User().apply {name = "yuichi"} println(user.name) } Very different... 全然違う…

Slide 13

Slide 13 text

case class User… object User { def apply(name: String): User = new User(name) def main(args: Array[String]) = { val user = User.apply(name = "yuichi") println(user.name) } } data class User… fun main() { val user = User().apply {name = "yuichi"} println(user.name) } KotlinはObjectがなくていい Top level function(Kotlin)

Slide 14

Slide 14 text

case class User… object User { def apply(name: String): User = new User(name) def main(args: Array[String]) = { val user = User.apply(name = "yuichi") println(user.name) } } data class User… fun main() { val user = User().apply {name = "yuichi"} println(user.name) } Apply function Scope function Kotlinのapplyはスコープ関数 Different function

Slide 15

Slide 15 text

How is Scala code decompiled? public void main(final String[] args) { User user = User$.MODULE$.apply("yuichi"); .MODULE$.println(user.name()); } Scalaをデコンパイルするとシンプル

Slide 16

Slide 16 text

How is Kotlin code decompiled? public final class UserKt { public static final void main() { User var1 = new User((String)null, 1, (DefaultConstructorMarker)null); ~~~ // some variables by scope function var1.setName("yuichi"); String var6 = var1.getName(); ~~~ System.out.println(var6); } public static void main(String[] var0) { main(); } } Kotlinをデコンパイルすると?

Slide 17

Slide 17 text

Top level function == ~~Kt class public final class UserKt { public static final void main() { User var1 = new User((String)null, 1, (DefaultConstructorMarker)null); ~~~ var1.setName("yuichi"); String var6 = var1.getName(); ~~~ System.out.println(var6); } public static void main(String[] var0) { main(); } } 関数をトップにするとXXXKtクラスが自動生成

Slide 18

Slide 18 text

How to show decompile code?(IntelliJ IDEA) IDEAでデコンパイル

Slide 19

Slide 19 text

How to show decompile code?(AS) ASでデコンパイル

Slide 20

Slide 20 text

Kotlin fun main() { val list = listOf(1) when (list) { is MutableList -> { val add = list += 2 println("$add") } } } // ??? Scala def main(args: ...): Unit = { val list = MutableList(1) list match { case MutableList(_) => { val add = list += 2 println(s"$add") } } } // ??? List Kotlin puzzlers https://github.com/angryziber/kotlin-puzzlers

Slide 21

Slide 21 text

Kotlin fun main() { val list = listOf(1) when (list) { is MutableList -> { val add = list += 2 println("$add") } } } // UnsupportedOperationException Scala def main(args: ...): Unit = { val list = MutableList(1) list match { case MutableList(_) => { val add = list += 2 println(s"$add") } } } // MutableList(1, 2) List

Slide 22

Slide 22 text

Kotlin fun main() { val list = listOf(1) // ArrayList!!! when (list) { is MutableList -> { val add = list += 2 println("$add") } } } // UnsupportedOperationException Scala def main(args: ...): Unit = { val list = MutableList(1) list match { case MutableList(_) => { val add = list += 2 println(s"$add") } } } // MutableList(1, 2) List KotlinのlistOfはArrayList

Slide 23

Slide 23 text

Kotlin cannot FP ??? Kotlinでは関数型ができないでしょうか

Slide 24

Slide 24 text

Arrow > Functional companion to Kotlin's Standard Library Option Either Try NonEmptyList Validated ... ArrowはKotlinの関数型実装をもつライブラリです

Slide 25

Slide 25 text

Arrow > Functional companion to Kotlin's Standard Library Option Either Try NonEmptyList Validated ... Default in Scala

Slide 26

Slide 26 text

Arrow > Functional companion to Kotlin's Standard Library Option Either Try NonEmptyList Validated ... Cats/Scalaz

Slide 27

Slide 27 text

Arrow > Functional companion to Kotlin's Standard Library Option Either Try NonEmptyList Validated ... Cats/Scalaz

Slide 28

Slide 28 text

Scala/Cats val list = NonEmptyList.of(1, 2, 3) val tail = list ++ List(100) println(s"${tail.exists(_ == 1)}") // ??? println(s"${tail.exists(_ == 100)}") // ??? Check element of NonEmptyList Kotlin/Arrow val list = NonEmptyList.of(1, 2, 3) val tail = list + listOf(100) println("${tail.contains(1)}") // ??? println("${tail.contains(100)}") // ??? CatsとArrowの要素チェック

Slide 29

Slide 29 text

The same results Kotlin/Arrow val list = NonEmptyList.of(1, 2, 3) val tail = list + listOf(100) println("${tail.contains(1)}") // true println("${tail.contains(100)}") // true Scala/Cats val list = NonEmptyList.of(1, 2, 3) val tail = list ++ List(100) println(s"${tail.exists(_ == 1)}") // true println(s"${tail.exists(_ == 100)}") // true 結果は同じ

Slide 30

Slide 30 text

Cats => Scalaz Kotlin/Arrow val list = NonEmptyList.of(1, 2, 3) val tail = list + listOf(100) println("${tail.contains(1)}") // true println("${tail.contains(100)}") // true Scala/Scalaz val list = NonEmptyList(1, 2, 3) val tail = list +: List(100) println(s"${tail.contains(1)}") // ??? println(s"${tail.contains(100)}") // ??? ScalazとArrow

Slide 31

Slide 31 text

NOT the same results :-o Kotlin/Arrow val list = NonEmptyList.of(1, 2, 3) val tail = list + listOf(100) println("${tail.contains(1)}") // true println("${tail.contains(100)}") // true Scala/Scalaz val list = NonEmptyList(1, 2, 3) val tail = list +: List(100) println(s"${tail.contains(1)}") // false println(s"${tail.contains(100)}") // treu チョット違う…

Slide 32

Slide 32 text

What is type? Kotlin/Arrow val list = NonEmptyList.of(1, 2, 3) val tail = list + listOf(100) println(tail) // ??? println("${tail.contains(1)}") // true println("${tail.contains(100)}") // true Scala/Scalaz val list = NonEmptyList(1, 2, 3) val tail = list +: List(100) println(tail) // ??? println(s"${tail.contains(1)}") // false println(s"${tail.contains(100)}") // treu tailの型は?

Slide 33

Slide 33 text

What is type? Kotlin/Arrow val list = NonEmptyList.of(1, 2, 3) val tail = list + listOf(100) println(tail) // NonEmptyList(all=[1, 2, 3, 100]) println("${tail.contains(1)}") // true println("${tail.contains(100)}") // true Scala/Scalaz val list = NonEmptyList(1, 2, 3) val tail = list +: List(100) println(tail) // List(NonEmpty[1,2,3], 100) println(s"${tail.contains(1)}") // false println(s"${tail.contains(100)}") // treu ListにCastされている

Slide 34

Slide 34 text

Wrap-up - Scala and Kotlin have similer syntax - But inside is quite different - Take care with similer types (ex. List type design) - Check decompile code 似てるけど中身は結構違う 似ている型は挙動が同じか確認しよう デコンパイルしよう

Slide 35

Slide 35 text

Thank you! Have a nice Scala & Kotlin! _kaelaela / Alp, Inc.