[jvmls 12] kotlin / java interop

33
Kotlin / Java Interop Andrey Breslav A Friend In Need Is a Friend Indeed 1 Wednesday, August 1, 12

Upload: andrey-breslav

Post on 13-May-2015

718 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: [JVMLS 12] Kotlin / Java Interop

Kotlin / Java Interop

Andrey Breslav

A Friend In Need Is a Friend Indeed

1Wednesday, August 1, 12

Page 2: [JVMLS 12] Kotlin / Java Interop

Kotlin

2

Modern Language for Industry

• Static typing

• Readability

• Tool support

• Interoperability

• Smart compiler

➡ Less boilerplate

• Flexible abstractions

➡ Powerful libraries

2Wednesday, August 1, 12

Page 3: [JVMLS 12] Kotlin / Java Interop

Stuff

• Apache 2

• Target➡ JVM (1.6)

➡ JavaScript

• Build➡ Ant

➡ Maven

• IDE➡ IntelliJ

➡ Web Demo

3* Planned

*

3Wednesday, August 1, 12

Page 4: [JVMLS 12] Kotlin / Java Interop

Outline

4

4Wednesday, August 1, 12

Page 5: [JVMLS 12] Kotlin / Java Interop

Outline• Enhancing Existing APIs

➡ Extensions

➡ Splitting Collections Interfaces

4

4Wednesday, August 1, 12

Page 6: [JVMLS 12] Kotlin / Java Interop

Outline• Enhancing Existing APIs

➡ Extensions

➡ Splitting Collections Interfaces

• Fighting for Safety➡ Nullable types

➡ Interaction with generics

4

4Wednesday, August 1, 12

Page 7: [JVMLS 12] Kotlin / Java Interop

Outline• Enhancing Existing APIs

➡ Extensions

➡ Splitting Collections Interfaces

• Fighting for Safety➡ Nullable types

➡ Interaction with generics

• Statics➡ Packages

➡ Class objects4

4Wednesday, August 1, 12

Page 8: [JVMLS 12] Kotlin / Java Interop

Making Existing Things Nicer

ExtensionsVariance Tricks

5Wednesday, August 1, 12

Page 9: [JVMLS 12] Kotlin / Java Interop

Extensions

• Demo

6

6Wednesday, August 1, 12

Page 10: [JVMLS 12] Kotlin / Java Interop

Collections & Variance

7

Java:

Kotlin:

7Wednesday, August 1, 12

Page 11: [JVMLS 12] Kotlin / Java Interop

Declaration-Site Variance

8

8Wednesday, August 1, 12

Page 12: [JVMLS 12] Kotlin / Java Interop

Translation

9

Øjava.util.List

9Wednesday, August 1, 12

Page 13: [JVMLS 12] Kotlin / Java Interop

Translation: Inheritance

10

10Wednesday, August 1, 12

Page 14: [JVMLS 12] Kotlin / Java Interop

Translation: Variance

11

11Wednesday, August 1, 12

Page 15: [JVMLS 12] Kotlin / Java Interop

Calling Java From Kotlin

12

12Wednesday, August 1, 12

Page 16: [JVMLS 12] Kotlin / Java Interop

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

13Wednesday, August 1, 12

Page 17: [JVMLS 12] Kotlin / Java Interop

Fighting NPEsNullable Types

Annotating Java Code

14Wednesday, August 1, 12

Page 18: [JVMLS 12] Kotlin / Java Interop

Nullable Types

• Demo

15

15Wednesday, August 1, 12

Page 19: [JVMLS 12] Kotlin / Java Interop

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

16Wednesday, August 1, 12

Page 20: [JVMLS 12] Kotlin / Java Interop

Annotations to The Rescue

17

JSR 305: Annotations for Software Defect Detection

String?String

17Wednesday, August 1, 12

Page 21: [JVMLS 12] Kotlin / Java Interop

Generics?!

18

@NotNulldoesn't

help

18Wednesday, August 1, 12

Page 22: [JVMLS 12] Kotlin / Java Interop

More Use-Cases of @Kotlin

19

19Wednesday, August 1, 12

Page 23: [JVMLS 12] Kotlin / Java Interop

External Annotations

20

20Wednesday, August 1, 12

Page 24: [JVMLS 12] Kotlin / Java Interop

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

21Wednesday, August 1, 12

Page 25: [JVMLS 12] Kotlin / Java Interop

StaticsPackages

Class Objects

22Wednesday, August 1, 12

Page 26: [JVMLS 12] Kotlin / Java Interop

Package-Level Code

23

file1.kt

file2.kt

foo/namespace.class

23Wednesday, August 1, 12

Page 27: [JVMLS 12] Kotlin / Java Interop

Package-Level Code

23

file1.kt

file2.kt

foo/namespace.class

Compiled From: file1.kt

23Wednesday, August 1, 12

Page 28: [JVMLS 12] Kotlin / Java Interop

Package-Level Code

24

file1.kt

file2.kt

foo/namespace.class

foo/namespace$file1.class

foo/namespace$file2.class

from:file1.kt

from:file2.kt

24Wednesday, August 1, 12

Page 29: [JVMLS 12] Kotlin / Java Interop

Package-Level Code

24

file1.kt

file2.kt

foo/namespace.class

foo/namespace$file1.class

foo/namespace$file2.class

from:file1.kt

from:file2.kt

Initialization Order?!

24Wednesday, August 1, 12

Page 30: [JVMLS 12] Kotlin / Java Interop

Kotlin ClassesHave No Statics

25Wednesday, August 1, 12

Page 31: [JVMLS 12] Kotlin / Java Interop

Class Object Translation

26

26Wednesday, August 1, 12

Page 32: [JVMLS 12] Kotlin / Java Interop

Class Object Translation

26

Only fields!

26Wednesday, August 1, 12

Page 33: [JVMLS 12] Kotlin / Java Interop

Resources

• Docs: http://kotlin.jetbrains.org

• Demo: http://kotlin-demo.jetbrains.com

• Code: http://github.com/jetbrains/kotlin

• Twitter:➡ @project_kotlin

➡ @abreslav

27

27Wednesday, August 1, 12