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

Writing Java-friendly Kotlin code (Mobius 2017)

Writing Java-friendly Kotlin code (Mobius 2017)

While Kotlin becomes more and more popular, lots of Java libraries are getting Kotlin helpers to make their usage in Kotlin code more idiomatic and clean. People, who’ve already tried Kotlin, understand that this language is much more pleasant to write code in than Java. Sure, one of the main selling sides of the language is its stunning Java interoperability. It’s really easy to call Java code from Kotlin, however, the opposite has its pitfalls.

In this talk we'll discuss, what tools Kotlin creators have prepared for such cases and how they will affect the resulting JVM bytecode. This talk will be useful not only for library creators, but also for developers who are hesitant to migrate their Java codebase.

Video: https://youtu.be/1L0q5VKx_-s

Sergey Ryabov

November 11, 2017
Tweet

More Decks by Sergey Ryabov

Other Decks in Programming

Transcript

  1. compile 'rxbinding-kotlin:x.y.x' compile 'rxbinding-design:x.y.x' compile 'autodispose-android-archcomponents-kotlin:x.y.z' compile 'autodispose-kotlin:x.y.z' compile 'autodispose-android-kotlin:x.y.z'

    compile 'autodispose:x.y.z' compile 'autodispose-android:x.y.z' compile 'autodispose-android-archcomponents:x.y.z' compile 'rxbinding:x.y.x' compile 'rxbinding-appcompat-v7:x.y.x' compile 'rxbinding-design-kotlin:x.y.x' compile 'rxbinding-appcompat-v7-kotlin:x.y.x'
  2. ANALYTICS ABSTRACTION LIBRARY object Analytics { fun send(event: Event) {}

    fun addPlugins(plugs: List<Plugin>) {} fun getPlugins(): List<Plugin> {} }
  3. ANALYTICS ABSTRACTION LIBRARY object Analytics { fun send(event: Event) {}

    fun addPlugins(plugs: List<Plugin>) {} fun getPlugins(): List<Plugin> {} } interface Plugin { fun init() fun send(event: Event) fun close() }
  4. ANALYTICS ABSTRACTION LIBRARY object Analytics { fun send(event: Event) {}

    fun addPlugins(plugs: List<Plugin>) {} fun getPlugins(): List<Plugin> {} } data class Event( val name: String, val context: Map<String, Any> = emptyMap() ) interface Plugin { fun init() fun send(event: Event) fun close() }
  5. USE-SITE VS DECLARATION-SITE VARIANCE // Java List<Dog> dogs = new

    ArrayList <>(); List<? extends Animal> animals = dogs;
  6. USE-SITE VS DECLARATION-SITE VARIANCE // Java List<Dog> dogs = new

    ArrayList <>(); List<? extends Animal> animals = dogs; // Kotlin val dogs: List<Dog> = ArrayList() val animals: List<Animal> = dogs
  7. USE-SITE VS DECLARATION-SITE VARIANCE // Java List<Dog> dogs = new

    ArrayList <>(); List<? extends Animal> animals = dogs; interface List<out E> : Collection<E> // Kotlin val dogs: List<Dog> = ArrayList() val animals: List<Animal> = dogs
  8. USE-SITE VS DECLARATION-SITE VARIANCE // Java List<Dog> dogs = new

    ArrayList <>(); List<? extends Animal> animals = dogs; interface List<out E> : Collection<E> // Kotlin val dogs: List<Dog> = ArrayList() val animals: List<Animal> = dogs
  9. MAY THE ANNOTATIONS BE WITH YOU • @JvmOverloads • @JvmStatic

    • @JvmField • @JvmWildcard @JvmSuppressWildcards • @Throws • @JvmName • @JvmMultifileClass
  10. WORTH NOTING • Inline functions • Reified type params •

    java.lang.Class • Unit • Typealiases
  11. WORTH NOTING • Inline functions • Reified type params •

    java.lang.Class • Unit • Typealiases • Visibility
  12. WORTH NOTING • Inline functions • Reified type params •

    java.lang.Class • Unit • Typealiases • internal visibility (!)
  13. WANT MORE? • Code from the talk https://goo.gl/ZWneG9 • Sinking

    Your Teeth Into Bytecode by Jake Wharton https://goo.gl/JLqFHr • Exploring Kotlin’s hidden costs  by Christophe Beyls https://goo.gl/xxLvWE