42
The Kotlin Programming Language Andrey Breslav Dmitry Jemerov Friday, July 29, 2011

The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

  • Upload
    others

  • View
    48

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

The Kotlin Programming Language

Andrey BreslavDmitry Jemerov

Friday, July 29, 2011

Page 2: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

What is Kotlin?• Statically typed

• object-oriented

• JVM-targeted

• general-purpose

• programming language

• developed by JetBrains➡ intended for industrial use

• Docs available today

• Public beta is planned for end of 2011

2

Friday, July 29, 2011

Page 3: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Goal-wise...

• Number of research papers we are planning to publish on Kotlin is➡ Zero

➡ ... or really close to that

3

Friday, July 29, 2011

Page 4: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Outline

• Motivation

• Feature overview

• Basic syntax

• Classes and Types

• Higher-order functions

• Type-safe Groovy-style Builders

4

Friday, July 29, 2011

Page 5: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Motivation• Why a new language?

➡ We are not satisfied with the existing ones

➡ And we have had a close look at many of them over 10 years

• Design goals➡ Full Java interoperability

➡ Compiles as fast as Java

➡ Safer than Java

➡ More concise than Java

➡ Way simpler than Scala5

Friday, July 29, 2011

Page 6: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Feature overview• Language features

➡ Static null-safety guarantees

➡ Higher-order functions ("closures")

➡ Mixins & First-class delegation

➡ Properties (no fields)

➡ Reified generics

➡ Declaration-site variance & "Type projections"

➡ Extension functions

➡ Modules and Build infrastructure

➡ Inline-functions (zero-overhead closures)

➡ Pattern matching

➡ ...

• Full-featured IDE by JetBrains from the very beginning

6

Friday, July 29, 2011

Page 7: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Basic syntax• IDE demo

➡ functions

➡ variables

➡ operator overriding

➡ extension functions

➡ null-safety

➡ automatic casts

➡ when-expressions

7

Friday, July 29, 2011

Page 8: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Hello, world!namespace demo1

fun main(args : Array<String>) : Unit { System.out?.println("Hello, world!")}

X

Friday, July 29, 2011

Page 9: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

String templatesnamespace demo2

fun main(args : Array<String>) { print("Hello, args' size is ${args.size}!")}

fun print(msg : String) { System.out?.println(msg)}

X

Friday, July 29, 2011

Page 10: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Assign-once localsfun main(args : Array<String>) { val text = "Hello, world!" print(text)}

fun print(s : String) { System.out?.println(s)}

X

Friday, July 29, 2011

Page 11: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

... and globalsval text = "Hello, world!"

fun main(args : Array<String>) { print(text)}

fun print(s : String) { System.out?.println(s)}

X

Friday, July 29, 2011

Page 12: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Local functionsfun main(args : Array<String>) { fun text() = "Hello, world!" print(text())}

fun print(message : String) { System.out?.println(message)}

X

Friday, July 29, 2011

Page 13: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Mutable variablesfun main(args : Array<String>) { var v = "Hello" v += ", " + "world!" print(v)}

fun print(message : String) { System.out?.println(message)}

X

Friday, July 29, 2011

Page 14: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Custom operatorsobject Console { fun plusAssign(s : String) { System.out?.println(s) }}

fun main(args : Array<String>) { var v = "Hello" v += ", " + "world!" Console += v}

X

Friday, July 29, 2011

Page 15: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Extension functionsfun main(args : Array<String>) { "Hello, world!".print()}

fun String.print() { System.out?.println(this)}

X

Friday, July 29, 2011

Page 16: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Null-safetyfun parseInt(s : String) : Int? { try { return Integer.parseInt(s) } catch (e : NumberFormatException) { return null }}

fun main(args : Array<String>) { val x = parseInt("123") val y = parseInt("Hello") x?.times(2)

if (x != null) { x.times(2) }}

X

Friday, July 29, 2011

Page 17: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Automatic casts and Whenfun foo(obj : Any?) { if (obj is String) { obj.get(0) } when (obj) { is String => obj.get(0) is Int => obj.plus(1) !is Boolean => null }}

fun bar(x : Int) { when (x) { 0 => "Zero" 1, 2, 3 => "1, 2 or 3" x+1 => "Really strange" in 10..100 => "In range" !in 100..1000 => "Out of range" }}

X

Friday, July 29, 2011

Page 18: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Types

8

SyntaxSyntax

Class types List<Foo>

Nullable types Foo?

Function types fun (Int) : String

Tuple types (Double, Double)

Self type This

Special typesSpecial types

Top Any?

Bottom Nothing

No meaningful return value Unit

Friday, July 29, 2011

Page 19: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Mapping to Java types

9

Kotlin Java Kotlin

Any Object Any?

Unit void Unit

Int int Int

Int? Integer Int?

String String String?

Array<Foo> Foo[] Array<Foo?>?

Array<Int> int[] Array<Int>?

Nothing - -

Foo Foo Foo?

GEN LOAD

Friday, July 29, 2011

Page 20: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Classesclass Foo(bar : Bar) : Buzz(bar) { ...}

• Any is the default supertype

• Constructors must initialize supertypes

• Final by default, explicit override annotations

10

Friday, July 29, 2011

Page 21: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Multiple inheritance?• Requirements

➡ Subtyping

➡ Implementation reuse

11

• Problems➡ Ambiguities

➡ Obscure initialization logic

• Unrestricted (C++)

• Interface-only (Java, C#)

• Traits (Scala)

• Mixins (Ada, CZ, ...)

Friday, July 29, 2011

Page 22: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Traits/Mixins (Envisioned)trait class Trait1 : Class1

with OtherTrait { // No state

}

class Foo(p : Bar) : Class1(p)with Trait1, Trait2 { ... }

class Decorator(p : Class3) : Class3 by pwith Trait1, Trait2 { ... }

12

Friday, July 29, 2011

Page 23: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Disambiguationtrait class A {

fun foo() : Int = 1 // virtual by default}

open class B() {virtual fun foo() : Int = 2

}

class C() : B with A {override fun foo() = this<A>.foo()

}

13

Friday, July 29, 2011

Page 24: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Automatic disambiguation

• If all the inherited implementations come from the same source (trait), there's no need to override?

• Issues➡ Binary compatibility

➡ Internal vs API

14

Friday, July 29, 2011

Page 25: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Generics (I)class Producer<out T> { fun produce() : T}

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

class Ouroboros<T> { fun consume(t : T) fun produce() : T}

Producer<Int> <: Producer<Any>

Consumer<Any> <: Consumer<Int>

Ouroboros<Int> >:< Ouroboros<Any>

15

Friday, July 29, 2011

Page 26: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Generics (II)

Ouroboros<out Int> <: Ouroboros<out Any>

• consume not available

Ouroboros<in Any> <: Ouroboros<in Int>

• produce on Ouroboros<in Int> returns Any?

16

Friday, July 29, 2011

Page 27: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Reified generics

• Type information in retained at runtime➡ foo is List<T>➡ Array<T>(3)➡ T.create()

• Java types are still erased➡ foo is java.util.List<*>

17

Friday, July 29, 2011

Page 28: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Class objects (I)

• Classes have no static members

• Each class may have a class object associated to it:

class Example() { class object { fun create() = Example() }}

val e = Example.create()

18

Friday, July 29, 2011

Page 29: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Class objects (II)

• Class objects can have supertypes:class Example() { class object : Factory<Example> { override fun create() = Example() }}

val factory : Factory<Example> = Exampleval e : Example = factory.create()

19

Friday, July 29, 2011

Page 30: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Class objects (III)

• Generic constraints for class objects:class Lazy<T>() where class object T : Factory<T> { private var store : T? = null public val value : T get() { if (store == null) { store = T.create() } return store }}

20

Friday, July 29, 2011

Page 31: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

First-class functions• Functions

➡ fun f(p : Int) : String

• Function types➡ fun (p : Int) : String➡ fun (Int) : String

• Function literals➡ {p => p.toString()}➡ {(p : Int) => p.toString()}➡ {(p : Int) : String => p.toString()}

21

Friday, July 29, 2011

Page 32: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Higher-order functions• filter(list, {s => s.length < 3})

➡ Sugar: last function literal argument✦ filter(list) {s => s.length < 3}

➡ Sugar: one-parameter function literal✦ filter(list) { it.length < 3 }

fun filter<T>(c : Iterable<T>, f : fun(T) : Boolean) : Iterable<T>

22

Friday, July 29, 2011

Page 33: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Lock example (I)myLock.lock()try {// Do something

}finally {myLock.unlock()

}

23

Friday, July 29, 2011

Page 34: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Lock example (II)

lock(myLock) {// Do something

}

fun lock(l : Lock, body : fun () : Unit)

24

Friday, July 29, 2011

Page 35: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Lock example (III)inline fun lock(l : Lock, body : fun () : Unit) {myLock.lock()try {body()

}finally {myLock.unlock()

}}

25

Friday, July 29, 2011

Page 36: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Extension functions• Functions

➡ fun Foo.f(p : Int) : String

• Function types➡ fun Foo.(p : Int) : String➡ fun Foo.(Int) : String

• Function literals➡ {Foo.(p : Int) => this.toString()}➡ {Foo.(p : Int) : String => this.toString()}

26

Friday, July 29, 2011

Page 37: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Builders in Groovyhtml { head { title "XML encoding with Groovy" } body { h1 "XML encoding with Groovy" p "this format can be used as an alternative markup to XML" /* an element with attributes and text content */ ahref:'http://groovy.codehaus.org' ["Groovy"] }}

27

Friday, July 29, 2011

Page 38: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Builders in Kotlinhtml { head { title { +"XML encoding with Kotlin" } } body { h1 { +"XML encoding with Kotlin" } p { +"this format is now type-safe" } /* an element with attributes and text content */ a(href="http://jetbrains.com/kotlin") { +"Kotlin" } }}

28

Friday, July 29, 2011

Page 39: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Builders: Implementation (I)• Function definition

fun html(init : fun HTML.() : Unit) : HTML { val html = HTML() html.init() return html}

• Usage

html { this.head { ... }}

29

Friday, July 29, 2011

Page 40: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Builders: Implementation (II)• Function definition

fun html(init : fun HTML.() : Unit) : HTML { val html = HTML() html.init() return html}

• Usage

html { head { ... }}

30

Friday, July 29, 2011

Page 41: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Builders: Implementation (III)abstract class Tag(val name : String) : Element { val children = ArrayList<Element>() val attributes = HashMap<String, String>() }

abstract class TagWithText(name : String) : Tag(name) { fun String.plus() { children.add(TextElement(this)) }}

class HTML() : TagWithText("html") { fun head(init : fun Head.() : Unit) { … } fun body(init : fun Body.() : Unit) { … }}

31

Friday, July 29, 2011

Page 42: The Kotlin Programming Language€¦ · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language • developed by JetBrains

Resources• Documentation:

➡ http://jetbrains.com/kotlin

• Blog:➡ http://blog.jetbrains.com/kotlin

• Twitter:➡ @project_kotlin

➡ @abreslav

➡ @intelliyole

32

Friday, July 29, 2011