2. What? Who? Why?
A modern language
Statically typed
Object-oriented
General-purpose
Compiler
JVM byte code
JavaScript (not GWT)
2
3. What? Who? Why?
A modern language
Statically typed
Object-oriented
General-purpose
Developed by JetBrains
Open source
Intended for industrial use
3
4. Dynamic vs static typing
Dynamically typed languages
Easier to learn
Easier to write small programs
Statically typed languages
Programs run much faster
More powerful IDE
Easier to support huge projects
4
5. Scala as a candidate
Goal
statically typed
JVM-targeted
concise language
Scala
too complicated
too implicit
too difficult to create a good IDE
5
6. Era of new industrial languages
Ceylon Red Hat / Jboss (April 2011)
Scala Typesafe (May 2011)
Kotlin JetBrains (July 2011)
xTend Eclipse / itemis (November 2011)
6
7. Kotlin “Hello, world”
fun main(args : Array<String>) {
Greeter().greet()
}
class Greeter(name : String = "world") {
val greeting = "Hello, $name!"
fun greet() {
println(greeting)
}
} 7
8. Problems and Solutions
Extending Existing APIs
Handling Absent Values
Case Analysis
Multiple Implementation Inheritance
Collection transformation
Variance of Generic Types
Builders
8
10. Regular expression example
Java
Pattern p = Pattern.compile("d*");
Matcher m = p.matcher("123");
if (m.find())
String result = m.group();
Scala
val r = "d*".r
val result = r.findFirstIn("123")
10
13. Scala: implicit conversions
val r = "d*".r
Conversion to 'StringOps' class:
implicit
requires a wrapper for each invocation
13
14. Functionality extension
Java
Collections.sort(list, comparator)
Scala / Kotlin
list.sort(comparator)
'sort' is not declared in java.util.List
'sort' can be autocompleted
14
19. Extension functions
fun <T> List<T>.sort(c:
Comparator<T>) {...}
fun String.iterator() =
StringIterator(this)
non-virtual
compile to a static function
19
20. Extending APIs
Scala
implicit
overhead on wrappers
Kotlin
functionality cannot be inherited
20
27. Scala: Option type
def getCustomer() : Option[Customer]
order.getCustomer.map(_.getInfo).
map(_.getAge)
extra objects, classes are created:
Some(customer), Some(info), Some(age)
classes for function literals
objects for function literals
inconvenient for debugging 27
38. Scala: pattern matching
sealed trait Expr
case class Num(value: Int) extends Expr
case class Sum(left: Expr, right: Expr)
extends Expr
def eval(e: Expr) : Int =
e match {
●
case Num(v) => v
case Sum(l, r) => eval(l) + eval(r)
}
38
39. Kotlin: smart casts
trait Expr
class Number(val value: Int) : Expr
class Sum(val left: Expr, val right: Expr):
Expr
fun eval(e: Expr) : Int {
if (e is Number)
return e.value //smart cast
if (e is Sum)
return eval(e.left) + eval(e.right)
throw IllegalArgumentException(
“Unknown expression $e”)
} 39
40. Kotlin: smart casts
trait Expr
class Num(val value: Int) : Expr
class Sum(val left: Expr, val right: Expr):
Expr
fun eval(e: Expr) : Int =
when (e) {
is Num > e.value
is Sum > eval(e.left) + eval(e.right)
else > throw IllegalArgumentException(
“Unknown expression $e”)
}
40
60. Scala. Declaration site variance
class List[+T] {
//only produces T
def head() : T
def tail() : List[T]
}
class Comparable[T] {
//only consumes T
def compare(t1 : T, t2: T)
} 60
61. Kotlin. Declaration site variance
class List<out T> {
//only produces T
fun head() : T
fun tail() : List[T]
}
class Comparable<in T> {
//only consumes T
fun compare(t1 : T, t2: T)
} 61
70. Builders: Implementation
Function definition
fun html(init : HTML.() > Unit) : HTML {
val html = HTML()
html.init()
return html
}
Usage
html {
this.head { ... }
} 70
71. Builders: Implementation
Function definition
fun html(init : HTML.() > Unit) : HTML {
val html = HTML()
html.init()
return html
}
Usage
html {
head { ... }
} 71
74. Resources
Home page
http://kotlin.jetbrains.org
Web Demo
http://kotlindemo.jetbrains.com
Blog
http://blog.jetbrains.com/kotlin
Forum
Kotlin at http://devnet.jetbrains.com 74
75. Interesting links
Marius Eriksen “Effective Scala”
http://twitter.github.com/effectivescala/
Yang Zhang “True Scala complexity”
http://yz.mit.edu/wp/truescalacomplexity/
Joshua Suereth D. “Scala in Depth”
http://www.amazon.com/ScalaDepthJoshuaSuereth
D/dp/1935182706
75