0% found this document useful (0 votes)
46 views27 pages

6-Kotlin Programming Language

The document discusses the Kotlin programming language, which is statically typed, JVM-targeted, and developed by JetBrains. It provides an overview of Kotlin's features like higher-order functions, properties, mixins, and extensions. It also demonstrates concepts like function literals, builders, and labeled returns through examples.

Uploaded by

Karl Tudtud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views27 pages

6-Kotlin Programming Language

The document discusses the Kotlin programming language, which is statically typed, JVM-targeted, and developed by JetBrains. It provides an overview of Kotlin's features like higher-order functions, properties, mixins, and extensions. It also demonstrates concepts like function literals, builders, and labeled returns through examples.

Uploaded by

Karl Tudtud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

The Kotlin

Programming Language
What is Kotlin?
• Statically typed
• JVM-targeted
• general-purpose
• programming language
• developed by JetBrains

• Docs available today


• Beta planned for the end of 2011
What is Kotlin? (II)
• Number of research papers we plan to
publish related to Kotlin?
– Zero
– Or close to that…
Outline
• Motivation
• Basic constructs walk-through
• Higher-order functions
– Function literals
– Inline functions
– Type-safe Groovy-style builders
– Non-local returns
• Workshop
– Classes, Mixins and First-class delegation
– Generics: Variance annotations and Type
projections
– Class objects
– Pattern matching
Motivation
• Why a new language?
– The existing ones do not fit with our needs
– And we had a close look at many of them
• Design goals
– Full Java interoperability
– Compiles as fast as Java
– Safer than Java
– More concise than Java
– Way simpler than Scala
Feature list
• Features:
– Higher-order functions
– Properties
– Mixins and First-class delegation
– Extension functions
– Static nullability checking
– Automatic casts
– Reified generics
– Declaration-site variance
– Modules and Build infrastructure (fighting the "Jar hell")
– Inline-functions (zero-overhead closures)
– Pattern matching
– …
• Full-featured IDE by JetBrains from the very beginning
Basic constructs walk-through
• IDE demo
Function types and literals
• Functions
fun f(p : Int) : String { return p.toString() }
• Function types
fun (p : Int) : String
fun (Int) : String
• Function literals
{(p : Int) : String => p.toString()}
{(p : Int) => p.toString()}
{p => p.toString()}
Higher-order functions
fun filter<T>(
c : Iterable<T>,
f : fun (T) : Boolean
) : Iterable<T>
val list = list("a", "ab", "abc", "")
filter(list, {s => s.length() < 3})
– yields ["a", "ab", ""]
– Convention: last function literal argument
filter(list) {s => s.length() < 3}
– Convention: one-parameter function literal
filter(list) {it.length() < 3}
Lock example (I)
myLock.lock()
try {
// Do something
} finally {
myLock.unlock()
}
Lock example (II)

lock (myLock) {
// Do something
}

fun lock(
theLock : Lock,
body : fun() : Unit
)
Implementation
• General: works everywhere, but costs
something
– Inner classes
– Method handles
• Special: may not work in some cases,
but costs nothing
– Inline functions
• Kotlin features both general and special
implementations
Lock example (III)
inline fun lock(
theLock : Lock,
body : fun() : Unit
) {
myLock.lock()
try {
body()
} finally {
myLock.unlock()
}
}
Extension function literals
• Extension functions
fun Int.f(p : Int) : String { return "…" }
• Extension function types
fun Int.(p : Int) : String
fun Int.(Int) : String
• Extension function literals
{Int.(p : Int) : String => "…"}
{Int.(p : Int) => "…"}
{Int.(p) => "…"}
HTML example (I)
• Function definition
fun html(init : fun HTML.() : Unit) : HTML {
val html = HTML()
html.init()
return html
}
• Usage
html {
this.addMeta(
httpEquiv="content-type",
content="text/html;charset=UTF-8")
}
HTML example (II)
• Function definition
fun html(init : fun HTML.() : Unit) : HTML {
val html = HTML()
html.init()
return html
}
• Usage
html {
addMeta(
httpEquiv="content-type",
content="text/html;charset=UTF-8")
}
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"]
}
Builders in Kotlin
html {
head {
title { +"XML encoding with Kotlin" }
}
body {
h1 { +"XML encoding with Kotlin" }
p { +"this format can be used as an alternative markup to XML" }

/* an element with attributes and text content */


a (href="http://jetbrains.com/kotlin") { +"Kotlin" }
}

• The big difference: the Kotlin version is statically type-checked


Builders in Kotlin: Implementation (I)
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) { … }
}
Builders in Kotlin: Implementation (II)
fun html(init : fun HTML.() : Unit) : HTML {
val html = HTML()
html.init()
return html
}

class HTML() : TagWithText("html") {

fun head(init : fun Head.() : Unit) {


val head = Head()
head.init()
children.add(head)
}

}
Builders in Kotlin: Implementation (III)
a (href="http://jetbrains.com/kotlin") { +"Kotlin" }

class BodyTag(name : String) : TagWithText(name) {

fun a(href : String, init : fun A.() : Unit) : A {


val a = A()
a.init()
a.attributes["href"] = href
children.add(a)
}

}
Foreach example (I)
inline fun <T> Iterable<T>.foreach(
body : fun(T) : Unit
) {
for (item in this)
body(item)
}

Example usage:
list map {it.length() > 2} foreach {
print(it)
}
Foreach example (II)
fun hasZero(list : List<Int>) : Boolean {
// A call to an inline function
list.foreach {
if (it == 0)
return true // Non-local return
}
return false
}

• Unqualified return always returns from a named


function
Qualified returns
• Function literals may be marked with labels:
@label {x => …}

• To make a local return, use qualified form:


@label { x =>

return@label

}
Labels, Break and Continue
@outer for (x in list1) {
for (y in list2) {
if (…) {
// Breaks the inner loop
break
}
if (…) {
// Breaks the outer loop
break@outer
}
}
}
Resources
• http://jetbrains.com/kotlin
• http://blog.jetbrains.com/kotlin
• @project_kotlin
• @intelliyole
• @abreslav
The Kotlin
Programming Language

You might also like