Share
Embed
Scala--pack your Future[T]; Kotlin is coming!
A presentation to Outbrain developers
oshai
July 12, 2017
Transcript
61 slides
-
Scala--pack your Future[T] Kotlin is coming! Ohad Shai Software
Developer && Team Leader @ Outbrain -
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 -
What’s wrong with Scala? • Readability sucks. Examples: – Collections
def map[B, That](f: A => B) (implicit bf: CanBuildFrom[Repr, B, That]): That -
What’s wrong with Scala? • Readability sucks. Examples: – Collections
– Implicit conversions -
What’s wrong with Scala? • Readability sucks. Examples: – Collections
– Implicit conversions – Operator overloading -
What’s wrong with Scala? • Readability sucks. Examples: – Collections
– Implicit conversions – Operator overloading – Underscore -
What’s wrong with Scala? • Scala has levels of features
• There is more than one way to do it -
What’s wrong with Scala? • Scala has levels of features
• There is more than one way to do it • Readability sucks -
What’s wrong with Scala? • Backward compatibility – “Scala isn’t
binary compatible between minor versions.” -
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] ------------------------------------------------------------------------ > -
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) > -
What’s wrong with Java? • Not much, it is just
old with lot of (extra) baggage -
What’s wrong with Java? • Not much, it is just
old with lot of (extra) baggage • Verbosity, Verbosity, Verbosity -
What’s wrong with Java? • Not much, it is just
old with lot of (extra) baggage • Verbosity, Verbosity, Verbosity • Velocity -
Our problems so far • Scala • Readability • Backward
compatibility • Compilation • Java • Not concise / Verbose • Velocity -
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 -
Why Kotlin? • Scala – the good parts • Practical
• Tools, support • Community • Google adopted it -
Basics • No new keyword • No Checked exceptions ☺
• == is equals (and we have ===) -
Basics • No new keyword • No Checked exceptions ☺
• == is equals (and we have ===) • No primitives -
Basics • No new keyword • No Checked exceptions ☺
• == is equals (and we have ===) • No primitives • Arrays are invariant (but default to Lists) -
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) -
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 } -
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) -
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}") -
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}") -
Fun fun appendToName(service: Service, toAppend: String): String { return "${service.name}-$toAppend"
} appendToName(service, "chidc2") -
Fun fun appendToName(service: Service, toAppend: String): String { return "${service.name}-$toAppend"
} appendToName(service = service, toAppend = "chidc2") -
Fun fun appendToName(service: Service, toAppend: String): String { return "${service.name}-$toAppend"
} ServiceUtils.appendToName(service, "chidc2") -
Fun fun Service.appendToName(toAppend: () -> String): String { return "${this.name}-${toAppend()}"
} service.appendToName("chidc2") -
Fun fun Service.appendToName(toAppend: () -> String): String { return "${this.name}-${toAppend()}"
} service.appendToName({ “chidc2” }) -
Fun fun Service.appendToName(toAppend: () -> String): String { return "${this.name}-${toAppend()}"
} service.appendToName { “chidc2” } -
Nullable Types val world: String? = null println(world.toUpperCase()) // In
Java you will get NPE // In Kotlin you will get compilation error -
Nullable Types val world: String? = null if (world !=
null) { println(world.toUpperCase()) } -
Data Class data class Campaign(val id: Int, val name: String)
// you will get: - toString() - equals() / hashCode() - copy() -
When Expression when (object) { 1 -> "One" "Hello" ->
"Greeting" is Long -> "Long" in 2..10 -> "obj is in the range" else -> "Unknown" } -
Collections val fruits = listOf("banana", "avocado", "apple", "kiwi") fruits .filter
{ it.startsWith("a") } .sortedBy { it } .map { it.toUpperCase() } .forEach { println(it) } -
Coroutines • Lightweight threads • Suspend-able computation • Writing imperative/blocking
style • Executed ‘similar’ to callback style -
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 -
Coroutines val jobs = List(100_000) { index -> async(CommonPool) {
delay(1000L) index } } println( jobs.sumBy { it.await() } ) -
Last minute bullets • Backward compatibility • Compilation Time •
Readability • Operator overloading • Underscore