Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Kotlin 1.2: Sharing code between platforms

186 views

Published on

Discover what's new in Kotlin 1.2: syntax, standard library, performance and , of course, "Multiplatform project". Take closer look at how Kotlin authors see way of reusing code between platforms.

Published in: Engineering
  • Be the first to comment

Kotlin 1.2: Sharing code between platforms

  1. 1. Kotlin 1.2 Sharing Code Between Platforms Kirill Rozov Android Developer
  2. 2. Kotlin Achievements * Based on information from Stackoverflow
 ** In development
  3. 3. Kotlin Achievements • Official language on Android * Based on information from Stackoverflow
 ** In development
  4. 4. Kotlin Achievements • Official language on Android • More than 17% projects in Android Studio 3.0 * Based on information from Stackoverflow
 ** In development
  5. 5. Kotlin Achievements • Official language on Android • More than 17% projects in Android Studio 3.0 • Support in Spring 5.0 * Based on information from Stackoverflow
 ** In development
  6. 6. Kotlin Achievements • Official language on Android • More than 17% projects in Android Studio 3.0 • Support in Spring 5.0 • Gradle Kotlin DSL * Based on information from Stackoverflow
 ** In development
  7. 7. Kotlin Achievements • Official language on Android • More than 17% projects in Android Studio 3.0 • Support in Spring 5.0 • Gradle Kotlin DSL • Fastest-growing and one of the least-disliked languages* * Based on information from Stackoverflow
 ** In development
  8. 8. Kotlin Achievements • Official language on Android • More than 17% projects in Android Studio 3.0 • Support in Spring 5.0 • Gradle Kotlin DSL • Fastest-growing and one of the least-disliked languages* • Support of JVM, JS, Native** * Based on information from Stackoverflow
 ** In development
  9. 9. Kotlin Achievements • Official language on Android • More than 17% projects in Android Studio 3.0 • Support in Spring 5.0 • Gradle Kotlin DSL • Fastest-growing and one of the least-disliked languages* • Support of JVM, JS, Native** • Kotlin Conf * Based on information from Stackoverflow
 ** In development
  10. 10. Kotlin Popularity 1.11.0
  11. 11. Language syntax
  12. 12. Array literals // Kotlin 1.1 @CacheConfig(cacheNames = arrayOf("books", "default")) class BookRepository
  13. 13. Array literals // Kotlin 1.1 @CacheConfig(cacheNames = arrayOf("books", "default")) class BookRepository The array literal syntax is constrained to annotation arguments // Kotlin 1.2 @CacheConfig(cacheNames = ["books", “default"]) class BookRepository
  14. 14. lateinit lateinit var lateinitVar: String // ‘false’ println("isInitialized: " + this::lateinitVar.isInitialized) lateinitVar = "value" // ‘true’ println("isInitialized: " + this::lateinitVar.isInitialized)
  15. 15. lateinit • Possibility to check whether lateinit variable is initialized • Support on top-level properties • Support for local variables
  16. 16. Inline functions // Kotlin 1.1 fun <E> Iterable<E>.strings() = strings { it.toString() } inline fun <E> Iterable<E>.strings(transform: (E) -> String) = map { transform(it) }
  17. 17. Inline functions // Kotlin 1.1 fun <E> Iterable<E>.strings() = strings { it.toString() } inline fun <E> Iterable<E>.strings(transform: (E) -> String) = map { transform(it) } // Kotlin 1.2 inline fun <E> Iterable<E>.strings( transform: (E) -> String = { it.toString() }) = map { transform(it) }
  18. 18. Casts improvements // Kotlin 1.1 val button = findViewById<View>(R.id.button) as Button
  19. 19. Casts improvements // Kotlin 1.1 val button = findViewById<View>(R.id.button) as Button // Kotlin 1.2 val button = findViewById(R.id.button) as Button
  20. 20. Casts improvements // Kotlin 1.1 val s : Any = … val firstChar = (s as? CharSequence)?.firstOrNull() if (firstChar != null) { s as CharSequence return s.count { it == firstChar } }
  21. 21. Casts improvements // Kotlin 1.2 if (firstChar != null) { // s: Any is smart cast to CharSequence return s.count { it == firstChar } } // Kotlin 1.1 val s : Any = … val firstChar = (s as? CharSequence)?.firstOrNull() if (firstChar != null) { s as CharSequence return s.count { it == firstChar } }
  22. 22. Standard library
  23. 23. Standard library
  24. 24. Standard library • Compatibility with Java 9 module system
  25. 25. Java 9 Support • Compatible with the Java 9 module system (Project Jigsaw)
 New artifacts kotlin-stdlib-jdkN • Deprecated declaration in kotlin.reflect package were removed
 Use declarations in kotlin.reflect.full package instead
  26. 26. Standard library • Compatibility with Java 9 module system
  27. 27. Standard library • Compatibility with Java 9 module system • New extensions for collections
  28. 28. chuncked, windowed, zipWithNext val items = listOf(1, 1, 2, 3) Only for Iterable<T>, Sequence<T>, CharSequence
  29. 29. chuncked, windowed, zipWithNext val items = listOf(1, 1, 2, 3) items.chunked(3) // [[1, 1, 2], [3]] Only for Iterable<T>, Sequence<T>, CharSequence
  30. 30. chuncked, windowed, zipWithNext val items = listOf(1, 1, 2, 3) items.chunked(3) // [[1, 1, 2], [3]] items.chunked(2) { (x, y) -> Pair(x, y) } // [(1, 1), (2, 3)] Only for Iterable<T>, Sequence<T>, CharSequence
  31. 31. chuncked, windowed, zipWithNext val items = listOf(1, 1, 2, 3) items.chunked(3) // [[1, 1, 2], [3]] items.chunked(2) { (x, y) -> Pair(x, y) } // [(1, 1), (2, 3)] items.windowed(2) // [[1, 1], [1, 2], [2, 3]] Only for Iterable<T>, Sequence<T>, CharSequence
  32. 32. chuncked, windowed, zipWithNext val items = listOf(1, 1, 2, 3) items.chunked(3) // [[1, 1, 2], [3]] items.chunked(2) { (x, y) -> Pair(x, y) } // [(1, 1), (2, 3)] items.windowed(2) // [[1, 1], [1, 2], [2, 3]] items.windowed(2) { it.average() } // [1, 1.5, 2.5] Only for Iterable<T>, Sequence<T>, CharSequence
  33. 33. chuncked, windowed, zipWithNext val items = listOf(1, 1, 2, 3) items.chunked(3) // [[1, 1, 2], [3]] items.chunked(2) { (x, y) -> Pair(x, y) } // [(1, 1), (2, 3)] items.windowed(2) // [[1, 1], [1, 2], [2, 3]] items.windowed(2) { it.average() } // [1, 1.5, 2.5] items.zipWithNext { a, b -> b - a } // [0, 1, 1] Only for Iterable<T>, Sequence<T>, CharSequence
  34. 34. fill, replaceAll, shuffle val items = mutableListOf(0, 1, 2, 3, 5, 8) Only for MutableList<T>
  35. 35. fill, replaceAll, shuffle val items = mutableListOf(0, 1, 2, 3, 5, 8) items.shuffle() // [3, 8, 5, 0, 2, 1]. Output can be different Only for MutableList<T>
  36. 36. fill, replaceAll, shuffle val items = mutableListOf(0, 1, 2, 3, 5, 8) items.shuffle() // [3, 8, 5, 0, 2, 1]. Output can be different items.replaceAll { it * 2 } // [0, 2, 4, 6, 10, 16] Only for MutableList<T>
  37. 37. fill, replaceAll, shuffle val items = mutableListOf(0, 1, 2, 3, 5, 8) items.shuffle() // [3, 8, 5, 0, 2, 1]. Output can be different items.replaceAll { it * 2 } // [0, 2, 4, 6, 10, 16] items.fill(5) // [5, 5, 5, 5, 5, 5] Only for MutableList<T>
  38. 38. Standard library • Fully compatibility with Java 9 module system • New extensions for collections
  39. 39. Standard library • Fully compatibility with Java 9 module system • New extensions for collections • Math operations
  40. 40. Standard library • Fully compatibility with Java 9 module system • New extensions for collections • Math operations • Operators and conversions for BigInteger and BigDecimal • Floating point to bits conversions • Regex is now Serializable • Closeable.use() calls Throwable.addSuppressed() (JDK 7+)
  41. 41. Java • Constructor calls normalization • Java-default methods calls • Breaking changes: • Consistent behaviour of x.equals(null) for platform types • Added check of receiver in inline extension functions that were called on a null value of a platform type • Smart cast inside try block after the block made more strict
  42. 42. Deprecation • Mutating backing field of read-only property • Override copy() in data classes • Not inner classes in enum entries • Passing a single item for a vararg parameter in the named form • Inner classes of generic classes extending Throwable
  43. 43. Other • Support for ::foo as a shorthand for this::foo • JS TypedArrays support for Kotlin primitive array (IntArray, FloatArray, etc) enabled by default • The Kotlin compiler now provides an option to treat all warnings as errors • Support in Kotlin Native 0.4
  44. 44. Compilation Performance Approximately 25% improvement over Kotlin 1.1
  45. 45. Multiplatform
 projects
  46. 46. Multiplatform Projects
  47. 47. expect & actual // expected platform-specific API expect fun hello(world: String) Common // actual JVM implementation actual fun hello(world: String) = println(“Hello, $world, on the JVM!") JVM // actual JS implementation actual fun hello(world: String) = console.log("Hello, $world, on the JS!") JS
  48. 48. expect class Date() { fun getDate(): Int fun getMonth(): Int } Common actual class Date { private val calendar: Calendar actual constructor() { calendar = Calendar.getInstance() } actual fun getDate() = calendar[DAY_OF_MONTH] actual fun getMonth() = calendar[MONTH] } JVM actual external class Date { actual fun getDate(): Int actual fun getMonth(): Int } JS
  49. 49. Common
 module Platform
 module Regular
 module
  50. 50. Common
 module JVM JS
  51. 51. //common-jvm/build.gradle apply plugin:'kotlin-platform-jvm' dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" expectedBy project(":common") } JVM //android/build.gradle apply plugin: 'kotlin-android' dependencies { implementation project(":common-jvm") } Android //common/build.gradle apply plugin: 'kotlin-platform-common' dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version" } Common
  52. 52. Common modules • Test
 Run test both on Java & JS platforms • Serialization
 Marshal Kotlin objects between different tiers of your application, based on JSON or ProtoBuf as serialization format • HTML
 The same code to render HTML in the backend and in the frontend
  53. 53. Future commons • IO • Networking (HTTP, TCP, etc.) • Dates
  54. 54. Summary
  55. 55. Summary • Allow to write parts of app in same language
  56. 56. Summary • Allow to write parts of app in same language • Interoperability on supported platforms
  57. 57. Summary • Allow to write parts of app in same language • Interoperability on supported platforms • Shared business logic
  58. 58. Summary • Allow to write parts of app in same language • Interoperability on supported platforms • Shared business logic • UI is platform-specific
  59. 59. Summary • Allow to write parts of app in same language • Interoperability on supported platforms • Shared business logic • UI is platform-specific • JVM/JS code reuse already worked, Native coming soon
  60. 60. Summary • Allow to write parts of app in same language • Interoperability on supported platforms • Shared business logic • UI is platform-specific • JVM/JS code reuse already worked, Native coming soon • Support of Multiplatform projects in IDEA 2017.3
  61. 61. Thanks krl.rozov@gmail.com

×