39
Challenges in JVM Language Design Andrey Breslav JetBrains

Kotlin: Challenges in JVM language design

Embed Size (px)

DESCRIPTION

Keynote at PPPJ 2013, Stuttgart, Germany

Citation preview

Page 1: Kotlin: Challenges in JVM language design

Challenges in JVM Language Design

Andrey Breslav JetBrains

Page 2: Kotlin: Challenges in JVM language design

http://kotlin.jetbrains.org

Page 3: Kotlin: Challenges in JVM language design

+

Page 4: Kotlin: Challenges in JVM language design

+Kotlin

n  Statically typed

n  Compiles to JVM byte codes or JavaScript

n  JVM version is Java compatible both ways

n  Intended for industrial use n  Designed for READABILITY n  Relies on simple (but powerful) abstractions n  Performance is a priority n  Tooling matters

n  Open Source (Apache 2) n  http://github.com/JetBrains/Kotlin

Page 5: Kotlin: Challenges in JVM language design

+Disclaimer

n  This talk is NOT an introduction to Kotlin

n  A few highlights J n  Extension functions

n  Type-Safe builders

n  Primary constructors

n  Delegated properties

n  Please refer to http://kotlin.jetbrains.org for more information

Page 6: Kotlin: Challenges in JVM language design

+Challenge 0

Compiler vs IDE

Page 7: Kotlin: Challenges in JVM language design

+IDE Services

Highlight file

Find Usages

Find Declaration

Page 8: Kotlin: Challenges in JVM language design

+Find Usages

Foo

File

File

File

Word Index

symbol

Foo

Foo

Foo

Foo

Foo

Foo

Foo

words

resolve

Page 9: Kotlin: Challenges in JVM language design

+2 in 1

Compiler • Analyze all code

(once)

IDE • Analyze one file • Resolve one reference

Page 10: Kotlin: Challenges in JVM language design

+

Page 11: Kotlin: Challenges in JVM language design

Java String s = null; s.length();

Errors At Runtime

Kotlin val s: String s.length() val s: String? = null s.length()

Errors At Compile Time

= null

Nullable type

Page 12: Kotlin: Challenges in JVM language design

Check and use val s: String? = … if (s != null) { s.length() }

Check and exit if (s == null) return s.length()

Rock’n’Roll s?.length()

Page 13: Kotlin: Challenges in JVM language design

+Java (as seen from Kotlin)

public class JavaClass { ! public String foo(List<String> l) {…} !} !

String

String?

List<String>

List<String?>

List<String>?

List<String?>?

Page 14: Kotlin: Challenges in JVM language design

+Annotations

public class JavaClass { ! @NotNull! public String foo(@NotNull List<String> l) {…} !} !

String

String?

List<String>

List<String?>

List<String>?

List<String?>?

Page 15: Kotlin: Challenges in JVM language design

+External Annotations

public class JavaClass { ! public String foo(List<String> l) {…} !} !

@NotNull @NotNull

annotations.xml

Page 16: Kotlin: Challenges in JVM language design

+

JAR File

class Foo { String getName() { … } }

Nullable or Not?

<XML/>

@NotNull

KAnnotator

Inferring annotations

Page 17: Kotlin: Challenges in JVM language design

+Inferring Annotations

n  Well-known problem n  A little out of fashion

n  Best tool to date: Julia by F. Spoto of Università di Verona

n  Expensive license

n  Challenge: n  Good open source implementation

n  Support for Generic Types

n  Performance

Page 18: Kotlin: Challenges in JVM language design

+Nullable types: Summary

Challenges •  What’s nullable

in Java?

Gains •  Static Type

Safety

Page 19: Kotlin: Challenges in JVM language design

How the Types Flow “Smart casts”

Page 20: Kotlin: Challenges in JVM language design

+Demo: Expression Evaluator

Page 21: Kotlin: Challenges in JVM language design

+Smart Casts and Vars

var x: String? = getNullableString() !! if (x != null && x.length > 0) { ! ... ! } !

Page 22: Kotlin: Challenges in JVM language design

+Smart Casts and Loops

var x: String? = getNullableString() !! if (x != null) { ! while (cond1) { ! x.length! if (cond2) { ! x = someNullableExpr! } ! else { ! ... ! } ! } ! } ! •  Backtracking

•  Restrictions

Page 23: Kotlin: Challenges in JVM language design

+Challenge

n  Fast algorithm for n  Flow-based typing

n  respecting bottom type,

n  mutable variables,

n  loops

n  and extension functions (not presented above)

Page 24: Kotlin: Challenges in JVM language design
Page 25: Kotlin: Challenges in JVM language design

JDK Collections interface List<E> { E get(int index); E set(int index, E value); }

Read-only /** read-only */ List<Foo> getFoos() { return unmodifiableList(l); }

Errors At Runtime

Page 26: Kotlin: Challenges in JVM language design

Cool Collections interface List<E> { E get(int index); } interface MutableList<E> extends List<E> { E set(int index, E value); }

Read-only List<Foo> getFoos() { return l; // may be mutable }

Errors At Compile Time

Page 27: Kotlin: Challenges in JVM language design

Safe Compatible

Cool Collections JDK Collections

catch errors early self-documenting code

no wrapping/repackaging existing API’s work

Intuitive ? extends Foo

print(List<? extends Foo> foos) print(foos: List<Foo>)

Page 28: Kotlin: Challenges in JVM language design

Iterable<out  T>  

Collec/on<out  T>  

List<out  T>   Set<out  T>  

MutableIterable<T>  

MutableCollec/on<T>  

MutableList<T>   MutableSet<T>  

java.lang.ArrayList<T>   java.u/l.HashSet<T>  

Kotlin Collections

JDK Collections

Page 29: Kotlin: Challenges in JVM language design

Rare luck

Kotlin Collections

Safe

Compatible

Co-variant

Page 30: Kotlin: Challenges in JVM language design

+Collections: Challenges

Infer? •  For Java

Classes

Generalize? •  To arbitrary

hierarchies

Page 31: Kotlin: Challenges in JVM language design

Generics

Page 32: Kotlin: Challenges in JVM language design

+List of Anything: Java

// Java !void print(Collection<Object> list) { ! for (Object item : list) { ! System.out.println(item); ! } !} !

List<Object>

Collection<String>

? extends Object

Page 33: Kotlin: Challenges in JVM language design

+Declaration-site variance

class Producer<out T> { ! fun produce(): T {…} !} !

fun print(p: Producer<Any>) { ! ... !} !

Producer<Any>

Producer<String>

•  Produces T •  Doesn’t consume T

Page 34: Kotlin: Challenges in JVM language design

+

class Producer<out T> { ! fun produce(): T {…} !} !

•  Covariant •  Produces T •  Doesn’t consume T

Co- and Contra-variance

class Consumer<in T> { ! fun consume(t: T) {…} !} !

•  Contravariant •  Doesn’t produce T •  Consumes T

class Box<T> { ! fun get(): T {…} ! fun set(t: T) {…} !} !

•  Invariant •  Produces T •  Consumes T

Page 35: Kotlin: Challenges in JVM language design

+Use-Site Variance

fun print(p: Array<Any>) { ! ... !} !

Array<Any>

Array<String>

66% wildcards can not be replaced by declaration-site

variance

Page 36: Kotlin: Challenges in JVM language design

+Use-Site Variance

fun print(p: Array<out Any>) { ! // Can’t call set() !} !

Array<Any>

Array<String>

Page 37: Kotlin: Challenges in JVM language design

+Mixed-Site Variance

Designed with the help from Ross Tate from Cornell University

http://www.cs.cornell.edu/~ross/publications/mixedsite/

Page 38: Kotlin: Challenges in JVM language design

+Summary

n  Type system design n  Generics

n  Flow-based typing

n  Interoperability n  Mapping Java types to Kotlin (and back)

n  Re-using Java collections

n  IDE vs Compiler n  Uniform implementation good for both scenarios

n  Incrementality

Page 39: Kotlin: Challenges in JVM language design

http://kotlin.jetbrains.org