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

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

oshai
July 12, 2017

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

A presentation to Outbrain developers

oshai

July 12, 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’s wrong with Scala? • Readability sucks. Examples: – Collections 

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

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

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

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

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

    install
 … [INFO] ApiTestSupport ........................................ SUCCESS [ 7.115 s]
 [INFO] AmplifyWafSim ......................................... SUCCESS [ 32.680 s]
 [INFO] Tracktor .............................................. SUCCESS [01:05 min]
 [INFO] AuthorizationSim ...................................... SUCCESS [ 1.080 s]
 [INFO] TracktorSim ........................................... SUCCESS [ 44.077 s]
 [INFO] BillingPocService ..................................... SUCCESS [ 1.234 s]
 [INFO] Shorty ................................................ SUCCESS [ 3.410 s]
 [INFO] Mushu ................................................. SUCCESS [ 26.661 s]
 [INFO] SpectorSim ............................................ SUCCESS [ 45.317 s]
 [INFO] WallStreetSim ......................................... SUCCESS [ 46.435 s]
 [INFO] AutoMarkerSim ......................................... SUCCESS [ 37.738 s]
 [INFO] BillingEngine ......................................... 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] ------------------------------------------------------------------------ >
  8. What’s wrong with Scala? • Compilation time > mvn clean

    install | grep min 
 [INFO] AdminSite .......................................... SUCCESS [ 5.005 s]
 [INFO] www ................................................ SUCCESS [01:05 min]
 [INFO] AmeliaLib .......................................... SUCCESS [01:30 min]
 [INFO] EditorialAdminProxy ................................ SUCCESS [ 2.704 s]
 [INFO] EditorialAdmin ..................................... SUCCESS [ 38.963 s]
 [INFO] Amelia ............................................. SUCCESS [02:29 min]
 [INFO] AmeliaReports ...................................... SUCCESS [01:16 min]
 [INFO] AmplifyApp ......................................... SUCCESS [01:17 min]
 [INFO] Sandbox ............................................ SUCCESS [01:16 min]
 [INFO] PVACEngine ......................................... SUCCESS [01:25 min]
 [INFO] PlasticRex ......................................... SUCCESS [01:22 min]
 [INFO] AmeliaSim .......................................... SUCCESS [02:21 min]
 [INFO] AmeliaReportsSim ................................... SUCCESS [01:05 min]
 [INFO] GateKeeper ......................................... SUCCESS [01:23 min]
 [INFO] DataScienceScaldingRunner .......................... SUCCESS [01:15 min]
 [INFO] GateKeeperSim ...................................... SUCCESS [01:28 min]
 [INFO] EventorSim ......................................... SUCCESS [01:13 min]
 [INFO] MarkerSim .......................................... SUCCESS [01:11 min]
 [INFO] Tracktor ........................................... SUCCESS [01:05 min]
 [INFO] Total time: 06:37 min (Wall Clock) >
  9. What’s wrong with Java? • Not much, it is just

    old with lot of (extra) baggage
  10. What’s wrong with Java? • Not much, it is just

    old with lot of (extra) baggage • Verbosity, Verbosity, Verbosity
  11. What’s wrong with Java? • Not much, it is just

    old with lot of (extra) baggage • Verbosity, Verbosity, Verbosity • Velocity
  12. Our problems so far • Scala • Readability • Backward

    compatibility • Compilation • Java • Not concise / Verbose • Velocity
  13. 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
  14. Why Kotlin? • Scala – the good parts • Practical

    • Tools, support • Community • Google adopted it
  15. Basics • No new keyword • No Checked exceptions ☺

    • == is equals (and we have ===)
  16. Basics • No new keyword • No Checked exceptions ☺

    • == is equals (and we have ===) • No primitives
  17. Basics • No new keyword • No Checked exceptions ☺

    • == is equals (and we have ===) • No primitives • Arrays are invariant (but default to Lists)
  18. Basics • No new keyword • No Checked exceptions ☺

    • == is equals (and we have ===) • No primitives • Arrays are invariant (but default to Lists) • Semicolons are optional (so don’t)
  19. Basics Java public class Service { private String name; public

    String getName() { return name; } public void setName(String name) { this.name = name; } } Kotlin class Service { var name: String? = null }
  20. Basics Java public class Service { private String name; public

    String getName() { return name; } public void setName(String name) { this.name = name; } } Kotlin class Service { var name: String? = null } val service = Service() println("Name is “ + service.name)
  21. Basics Java public class Service { private String name; public

    String getName() { return name; } public void setName(String name) { this.name = name; } } Kotlin class Service { var name: String? = null } val service = Service() println("Name is ${service.name}")
  22. Basics Java public class Service { private String name; public

    String getName() { return name; } public void setName(String name) { this.name = name; } } Kotlin class Service { var name: String? = null } val service = Service() println("Name is ${service.name}")
  23. Nullable Types val world: String? = null println(world.toUpperCase()) // In

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

    null) { println(world.toUpperCase()) }
  25. Data Class data class Campaign(val id: Int, val name: String)

    // you will get: - toString() - equals() / hashCode() - copy()
  26. When Expression when (object) { 1 -> "One" "Hello" ->

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

    { it.startsWith("a") } .sortedBy { it } .map { it.toUpperCase() } .forEach { println(it) }
  28. 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
  29. Coroutines val jobs = List(100_000) { index -> async(CommonPool) {

    delay(1000L) index } } println( jobs.sumBy { it.await() } )
  30. Last minute bullets • Backward compatibility • Compilation Time •

    Readability • Operator overloading • Underscore