JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Preview:

Citation preview

Practical Aspects of JVM Language Implementation

Andrey Breslav

Kotlin: How Things Work

Tuesday, October 2, 12

Why you should care• Language is an abstraction

➡ thus it leaks

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

2

abstraction

Tuesday, October 2, 12

3

tetris is way too easywhen played this way

Tuesday, October 2, 12

JVM & Its Languages

4

t1995

Scala,

Groovy, Closure, K

otlin...

Java

2012

What'spossible?

Tuesday, October 2, 12

About Me

• Project lead of Kotlin ➡ at JetBrains since 2010

• EG member of JSR-335➡ Project Lambda

5

(not (RICH HICKEY))

Tuesday, October 2, 12

Kotlin

6

Modern Language for Industry

• Static typing

• Readability

• Tool support

• Interoperability

• Smart compiler

➡ Less boilerplate

• Flexible abstractions

➡ Powerful libraries

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

10

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

(I)I

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

over

ride

s

"synthetic bridge method" (JLS)

Tuesday, October 2, 12

Kotlin Brides & Primitives

11

1

foo(t: T) = t

1

1

foo(t: Int) = t

1

Java&

KotlinKotlin

Tuesday, October 2, 12

Default Arguments

12

Tuesday, October 2, 12

Masks for Defaults

13

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

Tuesday, October 2, 12

Traits: Code in Interfaces

14

class Trait$Impl { static foo()V { println("Hi") }}

Tuesday, October 2, 12

Trait Hierarchy

15

T1

T2

C

foo() { println() }

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

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

Tuesday, October 2, 12

Summary

• Sometimes you need just one more method...

16

Tuesday, October 2, 12

CollectionsHow Data-Compatible is Your Language?

Tuesday, October 2, 12

Collections & Variance

18

Java:

Kotlin:

Tuesday, October 2, 12

Kotlin's Collections

19

Tuesday, October 2, 12

Declaration-Site Variance

20

Tuesday, October 2, 12

Translation

21

Øjava.util.List

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

Recommended