Difference between revisions of "Kotlin"
From zen2
(→Lambdas) |
(→Lambdas) |
||
| Line 56: | Line 56: | ||
<pre> | <pre> | ||
val sumA : (Int, Int) -> Int = {x,y -> x + y} | val sumA : (Int, Int) -> Int = {x,y -> x + y} | ||
| + | </pre> | ||
| + | |||
| + | |||
| + | |||
| + | Unit means returns nothing. In this example we do an async download: | ||
| + | <pre> | ||
| + | fun downloadData(url: String, completion: ()-> Unit) { | ||
| + | // sent a download request | ||
| + | // we got back data | ||
| + | // there were no network errors | ||
| + | completion() | ||
| + | } | ||
| + | |||
| + | // call downloadDataFunction | ||
| + | downloadData("fakeUrl.com",{ | ||
| + | println("The code in this block, will only run" + | ||
| + | "after the completion()") | ||
| + | }) | ||
| + | </pre> | ||
| + | |||
| + | |||
| + | |||
| + | Can use "it" if we only have one variable passed, it substitutes for that variable name: | ||
| + | <pre> | ||
| + | fun downloadCarData(url: String, completion: (Car) -> Unit) { | ||
| + | // send a download request | ||
| + | // we got back car data | ||
| + | val car = Car("Tesla", "ModelX","Blue") | ||
| + | completion(car) | ||
| + | } | ||
| + | |||
| + | downloadCarData("fakeUrl.com"){ car -> | ||
| + | println(car.color) | ||
| + | println(car.make) | ||
| + | } | ||
| + | |||
| + | downloadCarData("fakeUrl.com"){ | ||
| + | println(it.color) | ||
| + | println(it.make) | ||
| + | } | ||
</pre> | </pre> | ||
Revision as of 00:29, 19 November 2017
Contents
Class
Open
By default Kotlin classes are final, meaning no classes can inherit from them. In order to make them not final declare them open
open class Vehicle() {}
Override
To override class functions they must also be declared open
open class Vehicle(val make: String, val model: String) {
open fun drive() {}
}
class Car(make: String, model: String, val seats: Int) : Vehicle(make, model) {
override fun drive() {
}
}
Super
To use a function from the parent class use super
open class Vehicle() {
open fun drive() {
println("original")
}
}
class Car() : Vehicle() {
override fun drive() {
println("some stuff")
super.drive() //will append "original"
}
}
Lambdas
fun printMessage(message: String){
println(message)
}
Can be expressed as lambda
val printMessage = { message: String -> println(message)}
val sumA = {x: Int, y: Int -> x + y}
Can also be written
val sumA : (Int, Int) -> Int = {x,y -> x + y}
Unit means returns nothing. In this example we do an async download:
fun downloadData(url: String, completion: ()-> Unit) {
// sent a download request
// we got back data
// there were no network errors
completion()
}
// call downloadDataFunction
downloadData("fakeUrl.com",{
println("The code in this block, will only run" +
"after the completion()")
})
Can use "it" if we only have one variable passed, it substitutes for that variable name:
fun downloadCarData(url: String, completion: (Car) -> Unit) {
// send a download request
// we got back car data
val car = Car("Tesla", "ModelX","Blue")
completion(car)
}
downloadCarData("fakeUrl.com"){ car ->
println(car.color)
println(car.make)
}
downloadCarData("fakeUrl.com"){
println(it.color)
println(it.make)
}
