data class Person(val name: String, val age: Int? = null)
data class Person(val name: String, val age: Int? = null)
fun main(args: Array<String>) {
val persons = listOf(Person("Alice), Person("Bob", age = 29))
val oldest = persons.maxBy { it.age ?: 0 }
println("The oldest is: $oldest")
}
//The oldest is: Person(name=Bob, age=29)
Un DSL pour remplacer les layouts.
verticalLayout {
val name = editText()
button("Say hi!") {
onClick { toast("Hello, ${name.text}") }
}
}
fun nom(param1: TypeParam1, param2: String? = "def"): TypeRetour {
//Code de la fonction
return x
}
fun sum(a: Int, b: Int): Int = a + b
val result = if (condition) 1 else 2
return when (variable) {
is 1 -> "un"
is 2 -> "deux"
42 < 12 -> "The answer"
else -> "trop"
}
|
|
C’est la référence qu’on ne peut pas bouger, pas son contenu |
Force la vérification Null et Java (annotations, type non inférable)
|
|
LA KILLER FEATURE de Kotlin |
Permet de rajouter des méthodes après coup sur des classes existantes.
fun String.lastChar(): Char = this.get(this.length - 1)
"Hello".lastChar