advertisement

Kotlin: Challenges in JVM language design

50 %
50 %
advertisement
Information about Kotlin: Challenges in JVM language design
Technology

Published on September 12, 2013

Author: abreslav

Source: slideshare.net

Description

Keynote at PPPJ 2013, Stuttgart, Germany
advertisement

Challenges in JVM Language Design Andrey Breslav JetBrains

http://kotlin.jetbrains.org

+

+ 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

+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

+ Challenge 0 Compiler vs IDE

+ IDE Services Highlight file Find Usages Find Declaration

+ Find Usages Foo File File File Word Index symbol Foo Foo Foo Foo Foo Foo Foo words resolve

+ 2 in 1 Compiler •  Analyze all code (once) IDE •  Analyze one file •  Resolve one reference

+

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

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()

+ Java (as seen from Kotlin) public class JavaClass {! public String foo(List<String> l) {…}! }! String String? List<String> List<String?> List<String>? List<String?>?

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

+ External Annotations public class JavaClass {! public String foo(List<String> l) {…}! }! @NotNull @NotNull annotations.xml

+ JAR File class Foo { String getName() { … } } Nullable or Not? <XML/> @NotNull KAnnotator Inferring annotations

+ 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

+ Nullable types: Summary Challenges •  What’s nullable in Java? Gains •  Static Type Safety

How the Types Flow “Smart casts”

+ Demo: Expression Evaluator

+ Smart Casts and Vars var x: String? = getNullableString()! ! if (x != null && x.length > 0) {! ...! }!

+ Smart Casts and Loops var x: String? = getNullableString()! ! if (x != null) {! while (cond1) {! x.length! if (cond2) {! x = someNullableExpr! }! else {! ...! }! }! }! •  Backtracking •  Restrictions

+ 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)

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

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

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>)

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

Rare luck Kotlin Collections Safe Compatible Co-variant

+ Collections: Challenges Infer? •  For Java Classes Generalize? •  To arbitrary hierarchies

Generics

+ List of Anything: Java // Java! void print(Collection<Object> list) {! for (Object item : list) {! System.out.println(item);! }! }! List<Object> Collection<String> ? extends Object

+ 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

+ 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

+ Use-Site Variance fun print(p: Array<Any>) {! ...! }! Array<Any> Array<String> 66% wildcards can not be replaced by declaration-site variance

+ Use-Site Variance fun print(p: Array<out Any>) {! // Can’t call set()! }! Array<Any> Array<String>

+ Mixed-Site Variance Designed with the help from Ross Tate from Cornell University http://www.cs.cornell.edu/~ross/publications/mixedsite/

+ 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

http://kotlin.jetbrains.org

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

JetBrains introduces the new JVM language Kotlin - InfoQ.com

... the Czech company JetBrains introduced a programming language named Kotlin running on the JVM ... challenges in the Java language. JetBrains ... design ...
Read more

Kotlin: Challenges in JVM language design - Technology

Share Kotlin: Challenges in JVM language design.
Read more

Andrey Breslav - Kotlin: Challenges in language design ...

Curry On, Prague July 6th 2015 ... Kotlin: Challenges in language design - Curry On ... a Haskell for the JVM by Dierk König - Duration: ...
Read more

Kotlin: Challenges in language design - ECOOP 2015

Kotlin: Challenges in language design. ... Kotlin is a statically typed language compiled to JVM byte codes ... the lead language designer for Kotlin, ...
Read more

Kotlin: a new JVM language you should try - OpenCredo

Kotlin: a new JVM language ... a new programming language for the JVM. I say ‘new’, but Kotlin has ... One of the design goals behind Kotlin has always ...
Read more

The Advent of Kotlin: A Conversation with JetBrains ...

... a new statically typed language for the JVM ... of the challenges? ... and we've learned a lot from Scala's design. Compared to Kotlin, ...
Read more

Kotlin Native

Easy DSL design with Kotlin; Oct 28, ... Kotlin: a new JVM language you should ... It works for any JVM language and doesn’t really suffer from this ...
Read more

JVM Languages Report: Interview with Kotlin Creator ...

Interview with Kotlin Creator, Andrey Breslav ... RebelLabs: How did you come up with the idea of a new programming language for JVM? Andrey: ...
Read more