Kotlin: How Things Work
                           Practical Aspects of JVM Language
                                      Implementation



                                    Andrey Breslav



Tuesday, October 2, 12
Why you should care
                     •   Language is an abstraction
                         ➡   thus it leaks




       abstraction



                     •   When something weird happens
                         ➡   you may need to "see through the Matrix"


                                                   2
Tuesday, October 2, 12
tetris is way too easy
                         when played this way

                                   3
Tuesday, October 2, 12
JVM & Its Languages

                                                                                What's
                                                                    i n...
                                                              otl
                                                         e , K                 possible?
                                                      sur
                                                   Clo
                                               vy,
                                          G roo
                                      ,
                                Sc ala
                         Java

                                                                                           t
     1995                                                               2012

                                               4
Tuesday, October 2, 12
About Me

              •          Project lead of Kotlin
                    ➡      at JetBrains since 2010

                                                         (not
                                                           (RICH HICKEY))

              •          EG member of JSR-335
                    ➡      Project Lambda




                                                     5
Tuesday, October 2, 12
Kotlin
                         Modern Language for Industry

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




                                                      6
Tuesday, October 2, 12
Outline
                     • Extensions
                     • Bridges of All Sorts
                     • Types: Collections and Nulls

                     • Dreams... (maybe)

                                        7
Tuesday, October 2, 12
Extensions
                     • Live Demo
                         ➡   Extension syntax
                         ➡   How it is compiled
                         ➡   Multiple files in a package




                                                  8
Tuesday, October 2, 12
Bridges




                          9
Tuesday, October 2, 12
Bridges for Generics
                              (Lj/l/Object;)Lj/l/Object;




                                                            overrides
                              (I)I


                              (Lj/l/Object;)Lj/l/Object;

                                      "synthetic bridge method" (JLS)



                             10
Tuesday, October 2, 12
Kotlin Brides & Primitives
    Java                 1               1
     &                                        Kotlin
   Kotlin

      foo(t: T) = t                foo(t: Int) = t



                         1               1

                              11
Tuesday, October 2, 12
Default Arguments




                            12
Tuesday, October 2, 12
Masks for Defaults



                         foo(a, b)   foo(a, b, mask: Int)
                                       if (mask & 1)
                                         a = 0
                                       if (mask & 2)
                                         b = 0
                                       foo(a, b)


                                     13
Tuesday, October 2, 12
Traits: Code in Interfaces
                                   class Trait$Impl {
                                     static foo()V {
                                       println("Hi")
                                     }
                                   }




                              14
Tuesday, October 2, 12
Trait Hierarchy
                         T1   foo() { println() }


                         T2   foo() { T1$Impl.foo() }


                         C    foo() {
                                T2$Impl.foo()
                              }
                                      15
Tuesday, October 2, 12
Summary
                     • Sometimes you need just one more
                         method...




                                       16
Tuesday, October 2, 12
Collections
                         How Data-Compatible is Your Language?




Tuesday, October 2, 12
Collections & Variance
   Java:




   Kotlin:



                             18
Tuesday, October 2, 12
Kotlin's Collections




                              19
Tuesday, October 2, 12
Declaration-Site Variance




                             20
Tuesday, October 2, 12
Translation

                                   Ø
                                   java.util.List




                              21
Tuesday, October 2, 12
Translation: Inheritance




                              22
Tuesday, October 2, 12
Translation: Variance




                              23
Tuesday, October 2, 12
Calling Java From Kotlin




                             24
Tuesday, October 2, 12
Summary
                     • Kotlin's collections
                         ➡   Better than Java's
                         ➡   Still Compatible




                                                  25
Tuesday, October 2, 12
Nullable Types



Tuesday, October 2, 12
Demo
                     • Kotlin is null-safe
                     • Java code can be annotated for safe
                         access




                                        27
Tuesday, October 2, 12
Talk Summary
                     • Languages are about tradeoffs
                     • Kotlin is cool :)




                                       28
Tuesday, October 2, 12
Kotlin Resources
                 • Docs: http://kotlin.jetbrains.org
                 • Demo: http://kotlin-demo.jetbrains.com
                 • Code: http://github.com/jetbrains/kotlin
                 • Twitter:
                         ➡   @project_kotlin
                         ➡   @abreslav


                                               29
Tuesday, October 2, 12

JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

  • 1.
    Kotlin: How ThingsWork Practical Aspects of JVM Language Implementation Andrey Breslav Tuesday, October 2, 12
  • 2.
    Why you shouldcare • Language is an abstraction ➡ thus it leaks abstraction • When something weird happens ➡ you may need to "see through the Matrix" 2 Tuesday, October 2, 12
  • 3.
    tetris is waytoo easy when played this way 3 Tuesday, October 2, 12
  • 4.
    JVM & ItsLanguages What's i n... otl e , K possible? sur Clo vy, G roo , Sc ala Java t 1995 2012 4 Tuesday, October 2, 12
  • 5.
    About Me • Project lead of Kotlin ➡ at JetBrains since 2010 (not (RICH HICKEY)) • EG member of JSR-335 ➡ Project Lambda 5 Tuesday, October 2, 12
  • 6.
    Kotlin Modern Language for Industry • Smart compiler • Static typing ➡ Less boilerplate • Readability • Flexible abstractions • Tool support ➡ Powerful libraries • Interoperability 6 Tuesday, October 2, 12
  • 7.
    Outline • Extensions • Bridges of All Sorts • Types: Collections and Nulls • Dreams... (maybe) 7 Tuesday, October 2, 12
  • 8.
    Extensions • Live Demo ➡ Extension syntax ➡ How it is compiled ➡ Multiple files in a package 8 Tuesday, October 2, 12
  • 9.
    Bridges 9 Tuesday, October 2, 12
  • 10.
    Bridges for Generics (Lj/l/Object;)Lj/l/Object; overrides (I)I (Lj/l/Object;)Lj/l/Object; "synthetic bridge method" (JLS) 10 Tuesday, October 2, 12
  • 11.
    Kotlin Brides &Primitives Java 1 1 & Kotlin Kotlin foo(t: T) = t foo(t: Int) = t 1 1 11 Tuesday, October 2, 12
  • 12.
    Default Arguments 12 Tuesday, October 2, 12
  • 13.
    Masks for Defaults foo(a, b) foo(a, b, mask: Int) if (mask & 1) a = 0 if (mask & 2) b = 0 foo(a, b) 13 Tuesday, October 2, 12
  • 14.
    Traits: Code inInterfaces class Trait$Impl { static foo()V { println("Hi") } } 14 Tuesday, October 2, 12
  • 15.
    Trait Hierarchy T1 foo() { println() } T2 foo() { T1$Impl.foo() } C foo() { T2$Impl.foo() } 15 Tuesday, October 2, 12
  • 16.
    Summary • Sometimes you need just one more method... 16 Tuesday, October 2, 12
  • 17.
    Collections How Data-Compatible is Your Language? Tuesday, October 2, 12
  • 18.
    Collections & Variance Java: Kotlin: 18 Tuesday, October 2, 12
  • 19.
    Kotlin's Collections 19 Tuesday, October 2, 12
  • 20.
    Declaration-Site Variance 20 Tuesday, October 2, 12
  • 21.
    Translation Ø java.util.List 21 Tuesday, October 2, 12
  • 22.
    Translation: Inheritance 22 Tuesday, October 2, 12
  • 23.
    Translation: Variance 23 Tuesday, October 2, 12
  • 24.
    Calling Java FromKotlin 24 Tuesday, October 2, 12
  • 25.
    Summary • Kotlin's collections ➡ Better than Java's ➡ Still Compatible 25 Tuesday, October 2, 12
  • 26.
  • 27.
    Demo • Kotlin is null-safe • Java code can be annotated for safe access 27 Tuesday, October 2, 12
  • 28.
    Talk Summary • Languages are about tradeoffs • Kotlin is cool :) 28 Tuesday, October 2, 12
  • 29.
    Kotlin Resources • Docs: http://kotlin.jetbrains.org • Demo: http://kotlin-demo.jetbrains.com • Code: http://github.com/jetbrains/kotlin • Twitter: ➡ @project_kotlin ➡ @abreslav 29 Tuesday, October 2, 12