The Kotlin Programming Language Andrey Breslav Dmitry Jemerov - - PowerPoint PPT Presentation

the kotlin programming language
SMART_READER_LITE
LIVE PREVIEW

The Kotlin Programming Language Andrey Breslav Dmitry Jemerov - - PowerPoint PPT Presentation

The Kotlin Programming Language Andrey Breslav Dmitry Jemerov Thursday, July 28, 2011 What is Kotlin? Statically typed object-oriented JVM-targeted general-purpose programming language developed by JetBrains


slide-1
SLIDE 1

The Kotlin Programming Language

Andrey Breslav Dmitry Jemerov

Thursday, July 28, 2011

slide-2
SLIDE 2

What is Kotlin?

  • Statically typed
  • bject-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

Thursday, July 28, 2011

slide-3
SLIDE 3

Goal-wise...

  • Number of research papers we are

planning to publish on Kotlin is

➡ Zero ➡ ... or really close to that

3

Thursday, July 28, 2011

slide-4
SLIDE 4

Outline

  • Motivation
  • Feature overview
  • Basic syntax
  • Classes and Types
  • Higher-order functions
  • Type-safe Groovy-style Builders

4

Thursday, July 28, 2011

slide-5
SLIDE 5

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 Scala

5

Thursday, July 28, 2011

slide-6
SLIDE 6

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

Thursday, July 28, 2011

slide-7
SLIDE 7

Basic syntax

  • IDE demo

functions

variables

  • perator overriding

extension functions

null-safety

automatic casts

when-expressions

7

Thursday, July 28, 2011

slide-8
SLIDE 8

Hello, world!

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

X

Thursday, July 28, 2011

slide-9
SLIDE 9

String templates

namespace demo2 fun main(args : Array<String>) { print("Hello, args' size is ${args.size}!") } fun print(msg : String) { System.out?.println(msg) }

X

Thursday, July 28, 2011

slide-10
SLIDE 10

Assign-once locals

fun main(args : Array<String>) { val text = "Hello, world!" print(text) } fun print(s : String) { System.out?.println(s) }

X

Thursday, July 28, 2011

slide-11
SLIDE 11

... and globals

val text = "Hello, world!" fun main(args : Array<String>) { print(text) } fun print(s : String) { System.out?.println(s) }

X

Thursday, July 28, 2011

slide-12
SLIDE 12

Local functions

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

X

Thursday, July 28, 2011

slide-13
SLIDE 13

Mutable variables

fun main(args : Array<String>) { var v = "Hello" v += ", " + "world!" print(v) } fun print(message : String) { System.out?.println(message) }

X

Thursday, July 28, 2011

slide-14
SLIDE 14

Custom operators

  • bject Console {

fun plusAssign(s : String) { System.out?.println(s) } } fun main(args : Array<String>) { var v = "Hello" v += ", " + "world!" Console += v }

X

Thursday, July 28, 2011

slide-15
SLIDE 15

Extension functions

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

X

Thursday, July 28, 2011

slide-16
SLIDE 16

Null-safety

fun 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

Thursday, July 28, 2011

slide-17
SLIDE 17

Automatic casts and When

fun foo(obj : Any?) { if (obj is String) {

  • bj.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

Thursday, July 28, 2011

slide-18
SLIDE 18

Types

8

Syntax Class types List<Foo> Nullable types Foo? Function types fun (Int) : String Tuple types (Double, Double) Self type This Special types Top Any? Bottom Nothing No meaningful return value Unit

Thursday, July 28, 2011

slide-19
SLIDE 19

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

Thursday, July 28, 2011

slide-20
SLIDE 20

Classes

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

  • Any is the default supertype
  • Constructors must initialize supertypes
  • Final by default, explicit override annotations

10

Thursday, July 28, 2011

slide-21
SLIDE 21

Multiple inheritance?

  • Requirements

Subtyping

Implementation reuse

11

  • Problems

Ambiguities

Obscure initialization logic

  • Unrestricted (C++)
  • Interface-only (Java, C#)
  • Traits (Scala)
  • Mixins (Ada, CZ, ...)

Thursday, July 28, 2011

slide-22
SLIDE 22

Traits/Mixins (Envisioned)

trait class Trait1 : Class1 with OtherTrait { // No state } class Foo(p : Bar) : Class2(p) with Trait1, Trait2 { ... } class Decorator(p : Class3) : Class3 by p with Trait1, Trait2 { ... }

12

Thursday, July 28, 2011

slide-23
SLIDE 23

Disambiguation

trait class A { fun foo() : Int = 1 // virtual by default }

  • pen class B() {

virtual fun foo() : Int = 2 } class C() : B with A {

  • verride fun foo() = this<A>.foo()

}

13

Thursday, July 28, 2011

slide-24
SLIDE 24

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

Thursday, July 28, 2011

slide-25
SLIDE 25

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

Thursday, July 28, 2011

slide-26
SLIDE 26

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

Thursday, July 28, 2011

slide-27
SLIDE 27

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

Thursday, July 28, 2011

slide-28
SLIDE 28

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

Thursday, July 28, 2011

slide-29
SLIDE 29

Class objects (II)

  • Class objects can have supertypes:

class Example() { class object : Factory<Example> {

  • verride fun create() = Example()

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

19

Thursday, July 28, 2011

slide-30
SLIDE 30

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

Thursday, July 28, 2011

slide-31
SLIDE 31

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

Thursday, July 28, 2011

slide-32
SLIDE 32

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

Thursday, July 28, 2011

slide-33
SLIDE 33

Lock example (I)

myLock.lock() try { // Do something } finally { myLock.unlock() }

23

Thursday, July 28, 2011

slide-34
SLIDE 34

Lock example (II)

lock(myLock) { // Do something } fun lock(l : Lock, body : fun () : Unit)

24

Thursday, July 28, 2011

slide-35
SLIDE 35

Lock example (III)

inline fun lock(l : Lock, body : fun () : Unit) { myLock.lock() try { body() } finally { myLock.unlock() } }

25

Thursday, July 28, 2011

slide-36
SLIDE 36

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

Thursday, July 28, 2011

slide-37
SLIDE 37

Builders in Groovy

html { 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

Thursday, July 28, 2011

slide-38
SLIDE 38

Builders in Kotlin

html { 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

Thursday, July 28, 2011

slide-39
SLIDE 39

Builders: Implementation (I)

  • Function definition

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

  • Usage

html { this.head { ... } }

29

Thursday, July 28, 2011

slide-40
SLIDE 40

Builders: Implementation (II)

  • Function definition

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

  • Usage

html { head { ... } }

30

Thursday, July 28, 2011

slide-41
SLIDE 41

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

Thursday, July 28, 2011

slide-42
SLIDE 42

Resources

  • Documentation:

http://jetbrains.com/kotlin

  • Blog:

http://blog.jetbrains.com/kotlin

  • Twitter:

@project_kotlin

@abreslav

@intelliyole

32

Thursday, July 28, 2011