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

Scala--pack your Future[T]; Kotlin is coming! (Kotlin TLV)

oshai
August 14, 2017

Scala--pack your Future[T]; Kotlin is coming! (Kotlin TLV)

Many people ask why Kotlin is better than Scala, or sometimes claims the opposite. We will discuss all those extra features which are present in Scala but absent in Kotlin, and some more advantages of Kotlin. In this talk, we will also recap some of the awesome features that make Kotlin a great language to use.

oshai

August 14, 2017
Tweet

More Decks by oshai

Other Decks in Technology

Transcript

  1. About Me • Developing Software since 2005 • Mainly in

    Java and JVM languages • Works on backend systems • Maintainer of kotlin-logging open-source lib • Kotlin advocate on my spare time since early in 2016
  2. What is Kotlin • Statically typed Programming Language • Multi-Platform

    (JVM first, JS and Native) • Open Source • Developed by JetBrains since 2010 • Version 1.0 on Feb. 2016 • Official Android (Google) support since May 2017 https://medium.com/@octskyward/why-kotlin-is-my-next-programming-language-c25c001e26e3
  3. Why Kotlin? • Scala – the good parts (And also

    other languages) • Practical • Tools, support • Community • Google adopted it
  4. Side by side comparison Scala val hello: String = “hi"

    var hello: String = "hi" Kotlin val hello: String = “hi" var hello: String = "hi"
  5. Side by side comparison Scala val hello = “hi" var

    hello = "hi" Kotlin val hello = “hi" var hello = "hi"
  6. Case / data class Scala case class Service( name: String

    ) Kotlin data class Service( val name: String )
  7. Case / data class Scala case class Service( name: String

    ) // you will get: - toString() - equals() / hashCode() - copy() Kotlin data class Service( val name: String ) // you will get: - toString() - equals() / hashCode() - copy()
  8. Case / data class Scala case class Service( name: String

    ) Kotlin data class Service( val name: String ) // you will get: - toString() - equals() / hashCode() - copy() • Differences – new keyword – toString – Java interop (getter setter) – unapply
  9. Nullable Types val world: String? = null println(world.toUpperCase()) // In

    Java you will get NPE // In Kotlin you will get compilation error
  10. Nullable Types val world: String? = null if (world !=

    null) { println(world.toUpperCase()) }
  11. Implicit class / Extension method Scala object Helpers { implicit

    class IntMillis(x: Int) { def hours() = TimeUnit.HOURS.toMillis(x) } } … 5.hours() Kotlin fun Int.hours() = TimeUnit.HOURS.toMillis( this.toLong()) … 5.hours()
  12. What’s wrong with Scala? • Readability sucks. Examples: – Collections 

    //Kotlin fun <T, R> map(transform: (T) -> R): List<R>
  13. What’s wrong with Scala? • Readability sucks. Examples: – Collections 

    def map[B, That](f: A => B) (implicit bf: CanBuildFrom[Repr, B, That]): That
  14. What’s wrong with Scala? • Readability sucks. Examples: – Collections

    – Implicit conversions – Operator overloading
  15. What’s wrong with Scala? • Readability sucks. Examples: – Collections

    – Implicit conversions – Operator overloading – Underscore 
  16. What’s wrong with Scala? • Scala has levels of features

    • There is more than one way to do it
  17. What’s wrong with Scala? • Scala has levels of features

    • There is more than one way to do it • Readability sucks
  18. What’s wrong with Scala? • Compilation time > mvn clean

    install
 … [INFO] Module................................................. SUCCESS [ 7.115 s]
 [INFO] Module................................................. SUCCESS [ 32.680 s]
 [INFO] Module................................................. SUCCESS [01:05 min]
 [INFO] Module................................................. SUCCESS [ 1.080 s]
 [INFO] Module................................................. SUCCESS [ 44.077 s]
 [INFO] Module................................................. SUCCESS [ 1.234 s]
 [INFO] Module................................................. SUCCESS [ 3.410 s]
 [INFO] Module................................................. SUCCESS [ 26.661 s]
 [INFO] Module................................................. SUCCESS [ 45.317 s]
 [INFO] Module................................................. SUCCESS [ 46.435 s]
 [INFO] Module................................................. SUCCESS [ 37.738 s]
 [INFO] Module................................................. SUCCESS [ 14.198 s]
 [INFO] ------------------------------------------------------------------------
 [INFO] BUILD SUCCESS
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 06:37 min (Wall Clock)
 [INFO] Finished at: 2017-07-02T15:13:48+03:00
 [INFO] Final Memory: 970M/2498M
 [INFO] ------------------------------------------------------------------------ >
  19. What’s wrong with Scala? • Compilation time > mvn clean

    install | grep min 
 [INFO] Scala............................................... SUCCESS [01:05 min]
 [INFO] Scala............................................... SUCCESS [01:30 min]
 [INFO] Scala............................................... SUCCESS [02:29 min]
 [INFO] Scala............................................... SUCCESS [01:16 min]
 [INFO] Scala............................................... SUCCESS [01:17 min]
 [INFO] Scala............................................... SUCCESS [01:16 min]
 [INFO] Scala............................................... SUCCESS [01:25 min]
 [INFO] Scala............................................... SUCCESS [01:22 min]
 [INFO] Scala............................................... SUCCESS [02:21 min]
 [INFO] Scala............................................... SUCCESS [01:05 min]
 [INFO] Scala............................................... SUCCESS [01:23 min]
 [INFO] Scala............................................... SUCCESS [01:15 min]
 [INFO] Scala............................................... SUCCESS [01:28 min]
 [INFO] Scala............................................... SUCCESS [01:13 min]
 [INFO] Scala............................................... SUCCESS [01:11 min]
 [INFO] Scala............................................... SUCCESS [01:05 min]
 [INFO] Total time: 06:37 min (Wall Clock) >
  20. When Expression when (object) { 1 -> "One" "Hello" ->

    "Greeting" is Long -> "Long" in 2..10 -> "obj is in the range" else -> "Unknown" }
  21. Collections val fruits = listOf("banana", "avocado", "apple", "kiwi") fruits .filter

    { it.startsWith("a") } .sortedBy { it } .map { it.toUpperCase() } .forEach { println(it) }
  22. Coroutines • Lightweight threads • Suspend-able computation • Writing imperative/blocking

    style • Executed ‘similar’ to callback style • Implements: async/await, actors, channels, and more…
  23. Take Home • Scala • Readability • Backward compatibility •

    Compilation • Kotlin • A better practical Scala
  24. Coroutines val time = measureTimeMillis { val one = async(CommonPool)

    { doSomethingUsefulOne() } val two = async(CommonPool) { doSomethingUsefulTwo() } println("The answer is ${one.await() + two.await()}”) } println("Completed in $time ms") The answer is 42 Completed in 1017 ms
  25. Coroutines val jobs = List(100_000) { index -> async(CommonPool) {

    delay(1000L) index } } println( jobs.sumBy { it.await() } )