[jvmls 12] kotlin / java interop

Post on 13-May-2015

717 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Kotlin / Java Interop

Andrey Breslav

A Friend In Need Is a Friend Indeed

1Wednesday, August 1, 12

Kotlin

2

Modern Language for Industry

• Static typing

• Readability

• Tool support

• Interoperability

• Smart compiler

➡ Less boilerplate

• Flexible abstractions

➡ Powerful libraries

2Wednesday, August 1, 12

Stuff

• Apache 2

• Target➡ JVM (1.6)

➡ JavaScript

• Build➡ Ant

➡ Maven

• IDE➡ IntelliJ

➡ Web Demo

3* Planned

*

3Wednesday, August 1, 12

Outline

4

4Wednesday, August 1, 12

Outline• Enhancing Existing APIs

➡ Extensions

➡ Splitting Collections Interfaces

4

4Wednesday, August 1, 12

Outline• Enhancing Existing APIs

➡ Extensions

➡ Splitting Collections Interfaces

• Fighting for Safety➡ Nullable types

➡ Interaction with generics

4

4Wednesday, August 1, 12

Outline• Enhancing Existing APIs

➡ Extensions

➡ Splitting Collections Interfaces

• Fighting for Safety➡ Nullable types

➡ Interaction with generics

• Statics➡ Packages

➡ Class objects4

4Wednesday, August 1, 12

Making Existing Things Nicer

ExtensionsVariance Tricks

5Wednesday, August 1, 12

Extensions

• Demo

6

6Wednesday, August 1, 12

Collections & Variance

7

Java:

Kotlin:

7Wednesday, August 1, 12

Declaration-Site Variance

8

8Wednesday, August 1, 12

Translation

9

Øjava.util.List

9Wednesday, August 1, 12

Translation: Inheritance

10

10Wednesday, August 1, 12

Translation: Variance

11

11Wednesday, August 1, 12

Calling Java From Kotlin

12

12Wednesday, August 1, 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

13Wednesday, August 1, 12

Fighting NPEsNullable Types

Annotating Java Code

14Wednesday, August 1, 12

Nullable Types

• Demo

15

15Wednesday, August 1, 12

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

Annotations to The Rescue

17

JSR 305: Annotations for Software Defect Detection

String?String

17Wednesday, August 1, 12

Generics?!

18

@NotNulldoesn't

help

18Wednesday, August 1, 12

More Use-Cases of @Kotlin

19

19Wednesday, August 1, 12

External Annotations

20

20Wednesday, August 1, 12

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

StaticsPackages

Class Objects

22Wednesday, August 1, 12

Package-Level Code

23

file1.kt

file2.kt

foo/namespace.class

23Wednesday, August 1, 12

Package-Level Code

23

file1.kt

file2.kt

foo/namespace.class

Compiled From: file1.kt

23Wednesday, August 1, 12

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

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

Kotlin ClassesHave No Statics

25Wednesday, August 1, 12

Class Object Translation

26

26Wednesday, August 1, 12

Class Object Translation

26

Only fields!

26Wednesday, August 1, 12

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

top related