4. +
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
5. +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
11. 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
12. 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()
13. +
Java (as seen from Kotlin)
public class JavaClass {!
public String foo(List<String> l) {…}!
}!
String
String?
List<String>
List<String?>
List<String>?
List<String?>?
14. +
Annotations
public class JavaClass {!
@NotNull!
public String foo(@NotNull List<String> l) {…}!
}!
String
String?
List<String>
List<String?>
List<String>?
List<String?>?
16. +
JAR File
class Foo {
String getName() { … }
}
Nullable or Not?
<XML/>
@NotNull
KAnnotator
Inferring
annotations
17. +
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
34. +
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
35. +
Use-Site Variance
fun print(p: Array<Any>) {!
...!
}!
Array<Any>
Array<String>
66% wildcards
can not be replaced
by declaration-site
variance
38. +
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