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

Seamless Kotlin/Java Interop

pahill
April 17, 2019

Seamless Kotlin/Java Interop

Kotlin is fully interoperable with Java: you can call Java from Kotlin, and Kotlin from Java. While the former is straightforward, the latter can lead to more convoluted code as one has to incorporate Kotlin's new features. This talk will focus on making interop as seamless as possible, by highlighting common problems and give tips on how to handle them.

pahill

April 17, 2019
Tweet

More Decks by pahill

Other Decks in Programming

Transcript

  1. HELLO! I am Pamela Hill Android Engineer @ Luno You

    can find me at @pamelaahill or pamelaahill.com 2
  2. Topics ▹ Quick Kotlin 101 ▹ Why interop matters ▹

    Kotlin → Java ▹ Java → Kotlin ▹ Q&A 3
  3. 6 Declaring a variable val PI = 3.14 var radius

    = 13.0 • Forced to think of immutability vs mutability • Nudge toward favouring immutability
  4. 7 Type inference val isSquare = false val area =

    12.5 • The compiler is smart! • Type declarations only when cannot infer
  5. 8 Declaring a function fun plus(i: Int, j: Int): Int

    { return i + j } fun plus(i: Int, j: Int) = i + j • Expression body- one statement • Block bodies - more than one
  6. 9 Top-level functions and variables val PI = 3.14 fun

    plus(i: Int, j: Int) = i + j • Removes need for utilities classes
  7. 10 Named arguments fun plus(i: Int, j: Int) = i

    + j … plus(i = 1, j = 2) • Improves readability for long argument lists • Adds context
  8. 11 Nullability val width : Double = 4.0 val length

    : Double = 5.0 val height : Double? = null • Once possibly null, always possibly null • Guards against dreaded NPE
  9. 12 Data classes data class Rectangle (val width: Double, val

    height: Double) • Accessors, mutators, equals, hashCode, toString, copy implied • Concise, readable
  10. 13 Inheritance open class Rectangle (val width: Double, val height:

    Double) class Square (width: Double): Rectangle(width, width) • Need to open classes/functions for inheritance/overriding • Helps with fragile base class problem
  11. 14 Lambdas & higher-order functions val calculator = { i:

    Int, j: Int -> i + j} fun printCalculation (calc: (Int, Int) -> Int){ println("${calc(1, 2)}") } • Arrow library for more advanced features
  12. 15 Extension functions class Rectangle (val width: Double, val height:

    Double) … fun Rectangle.calcArea () = width * height … Rectangle(1,2).calcArea() • Alter existing classes without changing code
  13. 18 To whom should it matter? • Java library developers

    • Kotlin library developers • Mixed/migrating codebase developers
  14. 22 Don’t use hard keywords • object, in, is are

    keywords in Kotlin, but not Java • Kotlin will be forced to back-tick eg. `is`
  15. 23 Use nullability annotations • Every parameter, return, field should

    have a nullability annotation • Eg. @NotNull or @Nullable • Different annotation sets supported
  16. 24 Lambda parameters last fun calculation(x: Int, y: Int, calc:

    (Int, Int) -> Int) = calc(x,y) ... calculation( 3, 4) { i, j -> i * j } • Natural reading style, great for DSLs
  17. 25 Bean standards for properties class Rectangle { private int

    width = 0; public int getWidth() {…} public void setWidth(int width){…} } rectangle.width = 2; println("${rectangle.width}")
  18. 28 Files with top-level functions and variables In file CalcUtils.kt:

    fun calculation(x: Int, y: Int) = x * y CalcUtilsKt.calculation(3, 4)
  19. 29 Files with top-level functions and variables In file CalcUtils.kt:

    @file:JvmName("CalcUtils") fun calculation(x: Int, y: Int) = x * y CalcUtils.calculation(3, 4)
  20. 30 Exceptions fun parseInt(string: String) : Int { return string.toInt()

    } try { parseInt("abc") } catch (NumberFormatException nfe){…}
  21. 31 Exceptions @Throws(NumberFormatException::class) fun parseInt(string: String) : Int { return

    string.toInt() } try { parseInt("abc") } catch (NumberFormatException nfe){…}
  22. 32 Functions with defaults fun funWithDefaults(x: Int, y: Int =

    0, z: Int = 0) { ... } funWithDefaults(x,0,0)
  23. 33 Functions with defaults @JvmOverloads fun funWithDefaults(x: Int, y: Int

    = 0, z: Int = 0) { ... } funWithDefaults(x) funWithDefaults(x,y) funWithDefaults(x,y,z)
  24. 34 Unit returns fun sayHi(callback: (String) -> Unit) = {…}

    greeter.sayHi(name -> { System.out.println("Hello, " + name + "!"); return Unit.INSTANCE; });
  25. 35 Unit returns interface GreeterCallback { fun greetName(name: String): Unit

    } fun sayHi(callback: GreeterCallback) = {…} greeter.sayHi(name -> System.out.println("Greeting", "Hello, " + name + “!”));
  26. CREDITS Special thanks to all the people who made and

    released these awesome resources for free: ▹ Presentation template by SlidesCarnival ▹ Photographs by Unsplash and Pexels 39