Anton Rutkevich
JunoLab
GDG Minsk
/* Read-only */
val a: Int = 1
// `Int` type is inferred
val b = 1
// Type required when no initializer is provided
val c: Int
/* Read-write */
var x = 5 // `Int` type is inferred
x += 1
fun sum(a: Int, b: Int): Int {
return a + b
}
fun sum(a: Int, b: Int): Int = a + b
fun cases(obj: Any) {
when (obj) {
1 -> print("One")
"Hello" -> print("Greeting")
is Long -> print("Long")
!is String -> print("Not a string")
else -> print("Unknown")
}
}
class Invoice
class Customer(val name: String)
class Dog(name: String) {
init {
logger.info("Initialized with ${name}")
}
}
class MyView : View {
constructor(ctx: Context) : super(ctx) {
}
constructor(ctx: Context, attrs: AttributeSet)
: super(ctx, attrs) {
}
}
open class Base {
open fun canBeOverridden() {}
fun notOverrideable() {}
}
class Derived() : Base() {
override fun canBeOverridden() {}
}
class C {
companion object {
fun create() = C()
}
}
fun main() {
// C denotes the companion object here
val c = C.create()
// can be a value
val cCompanionObject = C.Companion
val cObject: C = cCompanionObject.create();
}
class Outer {
private val bar: Int = 1
class Nested {
fun foo() = 2
}
}
class Outer {
private val bar: Int = 1
inner class Inner {
fun foo() = bar
}
}
var <propertyName>: <PropertyType> [= <property_initializer>]
<getter>
<setter>
public class Address {
public var name: String = ...
var stringRepresentation: String
get() = this.toString()
set(value) {
// parses string into this object
setDataFromString(value)
}
}
// the initializer value is written
// directly to the backing field
var counter = 0
set(value) {
if (value >= 0)
$counter = value
}
// no backing field
val isEmpty: Boolean
get() = this.size == 0
var a: String = "abc"
a = null // compilation error
var b: String? = "abc"
b = null // ok
val l = a.length() // valid
val m = b.length() // error: variable 'b' can be null
// safe casts
b?.length()
// Elvis!
val n = b?.length() ?: -1
fun MutableList<Int>.swap(x: Int, y: Int) {
// 'this' corresponds to the list
val tmp = this[x]
this[x] = this[y]
this[y] = tmp
}
val l = arrayListOf(1, 2, 3)
// 'this' inside 'swap()' will hold the value of 'l'
l.swap(0, 2)
// function that takes lambda
fun synchronized(lock: Lock, body: () -> Unit): Unit {
lock.lock()
try {
body()
} finally {
lock.unlock()
}
}
// usage
synchronized (lock) {
doSomething()
}
fun <T, R> List<T>.map(transform: (T) -> R): List<R> {
val result = ArrayList<R>()
for (item in this)
result.add(transform(item))
return result
}
//usage
val ints = listOf(1, 2, 3)
val doubled = ints.map {it -> it * 2}
// also valid
val strings = listOf("one", "three", "five", "seven")
val transformed = strings filter {it.length == 5}
sortBy {it} map {it.toUpperCase()}
// inline function
inline fun synchronized(lock: Lock, body: () -> Unit): Unit {
...
}
// the code ..
synchronized(lock) { foo() }
// .. will be transformed to
lock.lock()
try {
foo()
} finally {
lock.unlock()
}
Library | Jar Size | Method Count |
---|---|---|
kotlin-runtime-0.11.91.4 | 363 KB | 952 |
kotlin-stdlib-0.11.91.4 | 656 KB | 6142 |
scala-library-2.11.5 | 5.3 MB | 50801 |
groovy-2.4.0-grooid | 4.5 MB | 29636 |
guava-18.0 | 2.2 MB | 14833 |
* Based on Jake Wharton's research (see links), updated
anton.rutkevich@gmail.com