A Friend In Need Is a Friend Indeed

                          Kotlin / Java Interop

                                    Andrey Breslav




Wednesday, August 1, 12                                         1
Kotlin
                          Modern Language for Industry

                          •    Smart compiler              •   Static typing
                              ➡ Less boilerplate           •   Readability
                          •    Flexible abstractions       •   Tool support
                              ➡ Powerful libraries         •   Interoperability




                                                       2
Wednesday, August 1, 12                                                           2
Stuff
               • Apache 2                  • IDE
               • Target                     ➡   IntelliJ
                   ➡      JVM (1.6)         ➡    Web Demo
                   ➡      JavaScript
                                                           *
               • Build
                   ➡      Ant
                   ➡      Maven
                                       3
                                            * Planned
Wednesday, August 1, 12                                        3
Outline




                          4
Wednesday, August 1, 12       4
Outline
               •    Enhancing Existing APIs
                   ➡      Extensions
                   ➡      Splitting Collections Interfaces




                                                 4
Wednesday, August 1, 12                                      4
Outline
               •    Enhancing Existing APIs
                   ➡      Extensions
                   ➡      Splitting Collections Interfaces

               •    Fighting for Safety
                   ➡      Nullable types
                   ➡      Interaction with generics




                                                 4
Wednesday, August 1, 12                                      4
Outline
               •    Enhancing Existing APIs
                   ➡      Extensions
                   ➡      Splitting Collections Interfaces

               •    Fighting for Safety
                   ➡      Nullable types
                   ➡      Interaction with generics

               •    Statics
                   ➡      Packages
                   ➡      Class objects
                                                 4
Wednesday, August 1, 12                                      4
Making Existing
                           Things Nicer
                                Extensions
                              Variance Tricks




Wednesday, August 1, 12                         5
Extensions
               • Demo




                          6
Wednesday, August 1, 12       6
Collections & Variance
  Java:




  Kotlin:



                          7
Wednesday, August 1, 12              7
Declaration-Site Variance




                          8
Wednesday, August 1, 12                 8
Translation

                              Ø
                              java.util.List




                          9
Wednesday, August 1, 12                        9
Translation: Inheritance




                          10
Wednesday, August 1, 12                10
Translation: Variance




                          11
Wednesday, August 1, 12             11
Calling Java From Kotlin




                          12
Wednesday, August 1, 12                12
Summary
               • Use JDK collections in Kotlin
               • Extend them with useful operations
               • Make them variant for Kotlin

               • At the expense of some unsoundness
                    around the edges


                                       13
Wednesday, August 1, 12                               13
Fighting NPEs
                               Nullable Types
                            Annotating Java Code




Wednesday, August 1, 12                            14
Nullable Types
               • Demo




                          15
Wednesday, August 1, 12        15
Nullable Types Summary
               •    null is a legitimate value, not a sin
               •    No overhead (as opposed to Option)
               •    Proper subtyping:
                   ➡      List<String> is a List<String?>



               •    At the expense of:

                                 file.getParent()!!.getName()!!

                                               16
Wednesday, August 1, 12                                           16
Annotations to The Rescue

                                          String   String?




       JSR 305: Annotations for Software Defect Detection

                               17
Wednesday, August 1, 12                                      17
Generics?!


                               @NotNull
                                doesn't
                                 help




                          18
Wednesday, August 1, 12                   18
More Use-Cases of @Kotlin




                          19
Wednesday, August 1, 12                 19
External Annotations




                          20
Wednesday, August 1, 12            20
It Is Not So Bad
               • IDE aids annotating with quick fixes
               • Some annotations can be inferred
                    automatically


               • Alternative: have a special kind of types
                    for Java things


                                      21
Wednesday, August 1, 12                                      21
Statics
                            Packages
                          Class Objects




Wednesday, August 1, 12                   22
Package-Level Code
        file1.kt
                               foo/namespace.class




        file2.kt




                          23
Wednesday, August 1, 12                              23
Package-Level Code
        file1.kt
                                 foo/namespace.class

                               Compiled From: file1.kt



        file2.kt




                          23
Wednesday, August 1, 12                                23
Package-Level Code
        file1.kt
                          foo/namespace.class
                                           foo/namespace$file1.class

                                                    from:
                                                   file1.kt

                                           foo/namespace$file2.class
        file2.kt                                     from:
                                                   file2.kt




                             24
Wednesday, August 1, 12                                          24
Package-Level Code
        file1.kt
                                foo/namespace.class
                                                   foo/namespace$file1.class

                                                            from:
                          Initialization Order?!           file1.kt

                                                   foo/namespace$file2.class
        file2.kt                                             from:
                                                           file2.kt




                                    24
Wednesday, August 1, 12                                                  24
Kotlin Classes
                          Have No Statics



Wednesday, August 1, 12                     25
Class Object Translation




                          26
Wednesday, August 1, 12                26
Class Object Translation

                               Only fields!




                          26
Wednesday, August 1, 12                      26
Resources
            • Docs: http://kotlin.jetbrains.org
            • Demo: http://kotlin-demo.jetbrains.com
            • Code: http://github.com/jetbrains/kotlin
            • Twitter:
                 ➡    @project_kotlin
                 ➡    @abreslav


                                        27
Wednesday, August 1, 12                                  27

[JVMLS 12] Kotlin / Java Interop

  • 1.
    A Friend InNeed Is a Friend Indeed Kotlin / Java Interop Andrey Breslav Wednesday, August 1, 12 1
  • 2.
    Kotlin Modern Language for Industry • Smart compiler • Static typing ➡ Less boilerplate • Readability • Flexible abstractions • Tool support ➡ Powerful libraries • Interoperability 2 Wednesday, August 1, 12 2
  • 3.
    Stuff • Apache 2 • IDE • Target ➡ IntelliJ ➡ JVM (1.6) ➡ Web Demo ➡ JavaScript * • Build ➡ Ant ➡ Maven 3 * Planned Wednesday, August 1, 12 3
  • 4.
    Outline 4 Wednesday, August 1, 12 4
  • 5.
    Outline • Enhancing Existing APIs ➡ Extensions ➡ Splitting Collections Interfaces 4 Wednesday, August 1, 12 4
  • 6.
    Outline • Enhancing Existing APIs ➡ Extensions ➡ Splitting Collections Interfaces • Fighting for Safety ➡ Nullable types ➡ Interaction with generics 4 Wednesday, August 1, 12 4
  • 7.
    Outline • Enhancing Existing APIs ➡ Extensions ➡ Splitting Collections Interfaces • Fighting for Safety ➡ Nullable types ➡ Interaction with generics • Statics ➡ Packages ➡ Class objects 4 Wednesday, August 1, 12 4
  • 8.
    Making Existing Things Nicer Extensions Variance Tricks Wednesday, August 1, 12 5
  • 9.
    Extensions • Demo 6 Wednesday, August 1, 12 6
  • 10.
    Collections & Variance Java: Kotlin: 7 Wednesday, August 1, 12 7
  • 11.
    Declaration-Site Variance 8 Wednesday, August 1, 12 8
  • 12.
    Translation Ø java.util.List 9 Wednesday, August 1, 12 9
  • 13.
    Translation: Inheritance 10 Wednesday, August 1, 12 10
  • 14.
    Translation: Variance 11 Wednesday, August 1, 12 11
  • 15.
    Calling Java FromKotlin 12 Wednesday, August 1, 12 12
  • 16.
    Summary • Use JDK collections in Kotlin • Extend them with useful operations • Make them variant for Kotlin • At the expense of some unsoundness around the edges 13 Wednesday, August 1, 12 13
  • 17.
    Fighting NPEs Nullable Types Annotating Java Code Wednesday, August 1, 12 14
  • 18.
    Nullable Types • Demo 15 Wednesday, August 1, 12 15
  • 19.
    Nullable Types Summary • null is a legitimate value, not a sin • No overhead (as opposed to Option) • Proper subtyping: ➡ List<String> is a List<String?> • At the expense of: file.getParent()!!.getName()!! 16 Wednesday, August 1, 12 16
  • 20.
    Annotations to TheRescue String String? JSR 305: Annotations for Software Defect Detection 17 Wednesday, August 1, 12 17
  • 21.
    Generics?! @NotNull doesn't help 18 Wednesday, August 1, 12 18
  • 22.
    More Use-Cases of@Kotlin 19 Wednesday, August 1, 12 19
  • 23.
    External Annotations 20 Wednesday, August 1, 12 20
  • 24.
    It Is NotSo Bad • IDE aids annotating with quick fixes • Some annotations can be inferred automatically • Alternative: have a special kind of types for Java things 21 Wednesday, August 1, 12 21
  • 25.
    Statics Packages Class Objects Wednesday, August 1, 12 22
  • 26.
    Package-Level Code file1.kt foo/namespace.class file2.kt 23 Wednesday, August 1, 12 23
  • 27.
    Package-Level Code file1.kt foo/namespace.class Compiled From: file1.kt file2.kt 23 Wednesday, August 1, 12 23
  • 28.
    Package-Level Code file1.kt foo/namespace.class foo/namespace$file1.class from: file1.kt foo/namespace$file2.class file2.kt from: file2.kt 24 Wednesday, August 1, 12 24
  • 29.
    Package-Level Code file1.kt foo/namespace.class foo/namespace$file1.class from: Initialization Order?! file1.kt foo/namespace$file2.class file2.kt from: file2.kt 24 Wednesday, August 1, 12 24
  • 30.
    Kotlin Classes Have No Statics Wednesday, August 1, 12 25
  • 31.
    Class Object Translation 26 Wednesday, August 1, 12 26
  • 32.
    Class Object Translation Only fields! 26 Wednesday, August 1, 12 26
  • 33.
    Resources • Docs: http://kotlin.jetbrains.org • Demo: http://kotlin-demo.jetbrains.com • Code: http://github.com/jetbrains/kotlin • Twitter: ➡ @project_kotlin ➡ @abreslav 27 Wednesday, August 1, 12 27