Kotlin Cheat Sheet 1p - by Ekito 1.2
Kotlin Cheat Sheet 1p - by Ekito 1.2
[email protected]
Cheat Sheet @arnogiu
Kotlin is an open source statically typed language for the JVM. It can run on Java 6+ and bring smart
features to make your code concise and safe. Its high interoperability helps to adopt it very quickly.
Official documentation can be found at http://kotlinlang.org/
GETTING STARTED
Gradle
buildscript {
ext.kotlin_version = '<version to use>'
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Android
In your build.gradle, use gradle setup and the android-kotlin plugins:
android {
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
Gradle Options
Compilation tuning in your gradle.properties file:
# Kotlin
kotlin.incremental=true
# Android Studio 2.2+
android.enableBuildCache=true
Maven
Refer to the documentation online : http://kotlinlang.org/docs/reference/using-maven.html
BASICS
package mypackage
import com.package
/** A block comment
*/
// line comment
by
[email protected]
@arnogiu
val a: Int = 1
val b = 1 // `Int` type is inferred
val c: Int // Type required when no initializer is provided
c=1 // definite assignment
Late variable initialization with lateinit. You can also look at lazy initialized values
lateinit var myString : String // lets you define a value later, but is considered as null if not set
val myValue : String by lazy { "your value ..." }
CLASSES
A simple Java POJO (Getter, Setter, Constructor) in one line
class User (
var firstName: String,
var lastName: String,
var address: String? = null
)
Data Class
By adding data keyword to your class, add toString(), equals(), copy() and exploded data (see
destructuring data below) capabilities
Kotlin by
[email protected]
Cheat Sheet @arnogiu
Properties
Properties can be declared in constructor or class body. You can also limit access to read (get)
or write (set) to any property.
// my resource singleton
object Resource {
// properties, functions
}
Closed Inheritance
The : operator, makes inheritance between classes and is allowed by opening it with open modi-
fier
FUNCTIONS
Function can be defined in a class (aka method) or directly in a package. Functions are declared
using the fun keyword
Default Values
Each parameter can have a default value
Named Arguments
When calling a function, you can freely set the given parameters by its order or by its name:
Function Extension
Kotlin allows you to define a function to add to an existing Class
Lambda
A lambda expression or an anonymous function is a function literal, i.e. a function that is not
declared, but passed immediately as an expression
- Its parameters (if any) are declared before -> (parameter types may be omitted),
Destructuring Data
Sometimes it is convenient to destructure an object into a number of variables. Here is the easy
way to return two values from a function:
when (s) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")
}
}
It can also be used for pattern matching, with expressions, ranges and operators (is, as )
COLLECTIONS
Collections are the same as the ones that come with Java. Be aware that Kotlin makes dier-
ence between immutable collections and mutables ones. Collection interfaces are immutable.
// immutable list
val list = listOf("a", "b", "c","aa")
list.filter { it.startsWith("a") }
Maps and Arrays can be accessed directly with [] syntax or with range expressions. Various of
methods on list/map can be used with lambdas expression :