29
Practical Aspects of JVM Language Implementation Andrey Breslav Kotlin: How Things Work Tuesday, October 2, 12

JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Embed Size (px)

Citation preview

Page 1: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Practical Aspects of JVM Language Implementation

Andrey Breslav

Kotlin: How Things Work

Tuesday, October 2, 12

Page 2: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

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

Page 3: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

3

tetris is way too easywhen played this way

Tuesday, October 2, 12

Page 4: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

JVM & Its Languages

4

t1995

Scala,

Groovy, Closure, K

otlin...

Java

2012

What'spossible?

Tuesday, October 2, 12

Page 5: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

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

Page 6: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Kotlin

6

Modern Language for Industry

• Static typing

• Readability

• Tool support

• Interoperability

• Smart compiler

➡ Less boilerplate

• Flexible abstractions

➡ Powerful libraries

Tuesday, October 2, 12

Page 7: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Outline

• Extensions

• Bridges of All Sorts

• Types: Collections and Nulls

• Dreams... (maybe)

7

Tuesday, October 2, 12

Page 8: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Extensions

• Live Demo➡ Extension syntax

➡ How it is compiled

➡ Multiple files in a package

8

Tuesday, October 2, 12

Page 9: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Bridges

9

Tuesday, October 2, 12

Page 10: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

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

Page 11: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Kotlin Brides & Primitives

11

1

foo(t: T) = t

1

1

foo(t: Int) = t

1

Java&

KotlinKotlin

Tuesday, October 2, 12

Page 12: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Default Arguments

12

Tuesday, October 2, 12

Page 13: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

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

Page 14: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Traits: Code in Interfaces

14

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

Tuesday, October 2, 12

Page 15: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Trait Hierarchy

15

T1

T2

C

foo() { println() }

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

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

Tuesday, October 2, 12

Page 16: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Summary

• Sometimes you need just one more method...

16

Tuesday, October 2, 12

Page 17: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

CollectionsHow Data-Compatible is Your Language?

Tuesday, October 2, 12

Page 18: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Collections & Variance

18

Java:

Kotlin:

Tuesday, October 2, 12

Page 19: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Kotlin's Collections

19

Tuesday, October 2, 12

Page 20: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Declaration-Site Variance

20

Tuesday, October 2, 12

Page 21: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Translation

21

Øjava.util.List

Tuesday, October 2, 12

Page 22: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Translation: Inheritance

22

Tuesday, October 2, 12

Page 23: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Translation: Variance

23

Tuesday, October 2, 12

Page 24: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Calling Java From Kotlin

24

Tuesday, October 2, 12

Page 25: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Summary

• Kotlin's collections➡ Better than Java's

➡ Still Compatible

25

Tuesday, October 2, 12

Page 26: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Nullable Types

Tuesday, October 2, 12

Page 27: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Demo

• Kotlin is null-safe

• Java code can be annotated for safe access

27

Tuesday, October 2, 12

Page 28: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

Talk Summary

• Languages are about tradeoffs

• Kotlin is cool :)

28

Tuesday, October 2, 12

Page 29: JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation

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