The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed •...
Transcript of The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed •...
![Page 1: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/1.jpg)
The Kotlin Programming Language
Andrey Breslav
Monday, October 3, 2011
![Page 2: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/2.jpg)
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 the end of 2011
2
Monday, October 3, 2011
![Page 3: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/3.jpg)
Goal-wise...
3
Monday, October 3, 2011
![Page 4: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/4.jpg)
Goal-wise...
• Number of research papers we are planning to publish on Kotlin is
3
Monday, October 3, 2011
![Page 5: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/5.jpg)
Goal-wise...
• Number of research papers we are planning to publish on Kotlin is➡ Zero
3
Monday, October 3, 2011
![Page 6: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/6.jpg)
Goal-wise...
• Number of research papers we are planning to publish on Kotlin is➡ Zero
➡ ... or really close to that
3
Monday, October 3, 2011
![Page 7: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/7.jpg)
Outline
• Motivation
• Feature overview
• Basic syntax
• Classes and Types
• Higher-order functions
• Type-safe Groovy-style Builders
4
Monday, October 3, 2011
![Page 8: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/8.jpg)
Motivation
5
Monday, October 3, 2011
![Page 9: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/9.jpg)
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
5
Monday, October 3, 2011
![Page 10: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/10.jpg)
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
Monday, October 3, 2011
![Page 11: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/11.jpg)
Feature overview
6
Monday, October 3, 2011
![Page 12: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/12.jpg)
Feature overview
6
Static n
ull-safe
ty guara
ntees
Higher-order functions ("closures")
Traits & First-class delegation
Propert
ies (no fi
elds)
Reified generics
Declaration-site variance & "Type projections"
Extension functions
Modules
and B
uild infra
structu
reInline-functions (zero-overhead closures)
Pattern matching
Monday, October 3, 2011
![Page 13: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/13.jpg)
Feature overview
6
Static n
ull-safe
ty guara
ntees
Higher-order functions ("closures")
Traits & First-class delegation
Propert
ies (no fi
elds)
Reified generics
Declaration-site variance & "Type projections"
Extension functions
Modules
and B
uild infra
structu
reInline-functions (zero-overhead closures)
Pattern matching
Full-featured IDE by JetBrains from the very beginning
Monday, October 3, 2011
![Page 14: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/14.jpg)
Code examples
7
• Functions
• Java interoperability
• String templates
• Local variables
• Type inference
• Extension functions and properties
• Null-safety
Monday, October 3, 2011
![Page 15: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/15.jpg)
Hello, world!namespace hello
fun main(args : Array<String>) : Unit { println("Hello, world!")}
fun println(message : String) { System.out?.println(message)}
8
Monday, October 3, 2011
![Page 16: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/16.jpg)
Hello, <names>!fun main(args : Array<String>) { var names : String = ""
for (i in args.indices) { names += args[i] if (i + 1 < args.size) names += ", " }
println("Hello, $names!")}
val Array<*>.indices : Iterable<Int> get() = IntRange<Int>(0, size - 1)
9
Monday, October 3, 2011
![Page 17: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/17.jpg)
Hello, <names>! (Faster version)
fun main(args : Array<String>) { val names = StringBuilder()
for (i in args.indices) { names += args[i] if (i + 1 < args.size) names += ", " }
println("Hello, $names!")}
fun StringBuilder.plusAssign(s : String) { this.append(s)}
10
Monday, October 3, 2011
![Page 18: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/18.jpg)
Hello, <names>! (Realistic version)
fun main(args : Array<String>) { println("Hello, ${args.join(", ")}!")}
fun <T> Iterable<T>.join(separator : String) : String { val names = StringBuilder() forit (this) { names += it.next() if (it.hasNext()) names += separator } return names.toString()}
11
Monday, October 3, 2011
![Page 19: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/19.jpg)
join() and forit()fun <T> Iterable<T>.join(separator : String) : String { val names = StringBuilder() forit (this) { names += it.next() if (it.hasNext()) names += separator } return names.toString()}
fun <T> forit(col : Iterable<T>, f : fun(Iterator<T>) : Unit) { val it = col.iterator() while (it.hasNext()) { f(it) }}
12
Monday, October 3, 2011
![Page 20: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/20.jpg)
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") print(x?.times(2)) // Can't say: print(x * 2)
if (x != null) { print(x * 2) }}
13
Monday, October 3, 2011
![Page 21: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/21.jpg)
Types
14
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
Monday, October 3, 2011
![Page 22: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/22.jpg)
Mapping to Java types
15
Kotlin Java Kotlin
Any Object Any?
Unit void Unit
Int int Int
Int? Integer Int?
String String String?
Array<Foo> Foo[] Array<Foo?>?
IntArray int[] IntArray?
Nothing - -
Foo Foo Foo?
GEN LOAD
Monday, October 3, 2011
![Page 23: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/23.jpg)
Automatic casts and Whenfun foo(obj : Any?) { if (obj is String) { obj.substring(2) } when (obj) { is String => obj[0] is Int => obj + 1 !is Boolean => null else => ... }}
16
Monday, October 3, 2011
![Page 24: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/24.jpg)
More on when-expressionsfun 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" }}
17
Monday, October 3, 2011
![Page 25: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/25.jpg)
Classes open class Parent(p : Bar) { open fun foo() {} fun bar() {} }
class Child(p : Bar) : Parent(p) { override fun foo() {} }
• Any is the default supertype
• Constructors must initialize supertypes
• Final by default, explicit override annotations
18
Monday, October 3, 2011
![Page 26: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/26.jpg)
Traitstrait T1 : Class1, OtherTrait {
// No state
fun foo() : Int = 1 // open by default
fun bar() : Int // abstract by default
}
class Foo(p : Bar) : Class1(p), T1, T2 { override fun bar() : Int = foo() + 1
}
19
Monday, October 3, 2011
![Page 27: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/27.jpg)
Disambiguationtrait A {
fun foo() : Int = 1
}
open class B() {
open fun foo() : Int = 2
}
class C() : B(), A {
override fun foo() = super<A>.foo()
}
20
Monday, October 3, 2011
![Page 28: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/28.jpg)
First-class Delegationtrait List<T> {
fun add(t : T)
fun get(index : Int) : T
}
class ListDecorator<T>(p : List<T>) : List<T> by p {
override fun add(t : T) {
log.message("Added $t")
super.add(t)
}
// override fun get(index : Int) : T = super.get()
}
21
Monday, October 3, 2011
![Page 29: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/29.jpg)
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()}
22
Monday, October 3, 2011
![Page 30: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/30.jpg)
Higher-order functionsfun <T> filter( c : Iterable<T>, f : fun(T) : Boolean) : Iterable<T>
• 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 }
23
Monday, October 3, 2011
![Page 31: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/31.jpg)
Infix function calls & "LINQ"a.contains(b)
// is the same as
a contains b
users
.filter { it hasPrivilege WRITE }
.map { it => it.fullName }
.orderBy { lastName }
24
Monday, October 3, 2011
![Page 32: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/32.jpg)
Lock example (I)myLock.lock()
try {
// Do something
}
finally {
myLock.unlock()
}
25
Monday, October 3, 2011
![Page 33: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/33.jpg)
Lock example (II)
lock(myLock) {
// Do something
}
fun lock(l : Lock, body : fun () : Unit)
26
Monday, October 3, 2011
![Page 34: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/34.jpg)
Lock example (III)inline fun lock(l : Lock, body : fun () : Unit) {
l.lock()
try {
body()
}
finally {
l.unlock()
}
}
27
Monday, October 3, 2011
![Page 35: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/35.jpg)
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()}
28
Monday, October 3, 2011
![Page 36: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/36.jpg)
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"]
}
}
29
Monday, October 3, 2011
![Page 37: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/37.jpg)
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" }
}
}
30
Monday, October 3, 2011
![Page 38: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/38.jpg)
Builders: Implementation (I)• Function definition
fun html(init : fun HTML.() : Unit) : HTML { val html = HTML() html.init() return html}
• Usage
html { this.head { ... }}
31
Monday, October 3, 2011
![Page 39: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/39.jpg)
Builders: Implementation (II)• Function definition
fun html(init : fun HTML.() : Unit) : HTML { val html = HTML() html.init() return html}
• Usage
html { head { ... }}
32
Monday, October 3, 2011
![Page 40: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/40.jpg)
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() : Tag("html") { fun head(init : fun Head.() : Unit) { } fun body(init : fun Body.() : Unit) { }}
33
Monday, October 3, 2011
![Page 41: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/41.jpg)
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" }
}
}
34
Monday, October 3, 2011
![Page 42: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/42.jpg)
Generics: Invarianceclass List<T> {
fun add(t : T)
fun get(index : Int) : T
}
val ints = List<Int>()
val anys : List<Any> = ints
anys.add("1") // Cause of the problem
val i : Int = ints.get(0) // !!!
35
Monday, October 3, 2011
![Page 43: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/43.jpg)
Generics: Declaration-site variance
class List<T> {
fun add(t : T)
fun get() : T
}
class Producer<out T> {
fun get() : T
}
class Consumer<in T> {
fun add(t : T)
}
List<Int> >:< List<Any>
val ints = List<Int>()
val anys : List<Any> = ints
val ints = Producer<Int>()
val anys : Producer<Any> = ints
val anys = Consumer<Any>()
val ints : Consumer<Int> = anys
36
Monday, October 3, 2011
![Page 44: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/44.jpg)
Generics: Use-site varianceval ints = List<Int>()
val anysOut : List<out Any> = ints
anysOut.add("1") // Not available
val i : Int = ints.get() // No problem
37
Monday, October 3, 2011
![Page 45: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/45.jpg)
Generics: Use-site varianceval ints = List<Int>()
val anysOut : List<out Any> = ints
anysOut.add("1") // Not available
val i : Int = ints.get() // No problem
37
val anys = List<Any>()
val intsIn : List<in Int> = anys
intsIn.add(0)
val obj = intsIn.get() // : Any?
Monday, October 3, 2011
![Page 46: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/46.jpg)
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<*>
38
Monday, October 3, 2011
![Page 47: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/47.jpg)
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()
39
Monday, October 3, 2011
![Page 48: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/48.jpg)
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()
40
Monday, October 3, 2011
![Page 49: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/49.jpg)
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 }}
41
Monday, October 3, 2011
![Page 50: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/50.jpg)
We are hiring
• Full-time➡ Back-end development
➡ Standard library development
➡ Static analyses and Refactorings
➡ Incremental compilation
• Internships
42
Monday, October 3, 2011
![Page 51: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/51.jpg)
Resources
• Documentation:➡ http://jetbrains.com/kotlin
• Blog:➡ http://blog.jetbrains.com/kotlin
• Twitter:➡ @project_kotlin
➡ @abreslav
43
Monday, October 3, 2011
![Page 52: The Kotlin Programming Language - Computer Science … · What is Kotlin? • Statically typed • object-oriented • JVM-targeted • general-purpose • programming language •](https://reader036.fdocuments.net/reader036/viewer/2022062909/5b4aeb6c7f8b9aa82c8c7bfe/html5/thumbnails/52.jpg)
Practical Type Systems (seminar)
• Topics➡ Type systems of industrial languages (e.g. C#, Java, Scala, Kotlin)
➡ Cutting-edge work on OOP and Generic programming
➡ Formalizing Kotlin
• Location➡ JetBrains, Kantemirovskaya, 2A (m. "Lesnaya")
• Day/Time➡ TBD
• To participate➡ [email protected]
44
Monday, October 3, 2011