Introduction
Kotlin itself is a very powerful tool, but many often do not use it to its full capacity, turning it into some kind of ... Java 6. I'll try to tell you why you shouldn't do this and how to use the functional features of the language to the fullest.
Higher order functions
I'll start with them. And, at the same time, I'll tell you what it is all about: when a function takes another function as a parameter or returns it, it is a higher-order function. Simple, isn't it? But how do you use it?
The fact that functions in Kotlin can receive and return other functions for us should mean that we can write them to a variable. It will look something like this:
val foo: () -> Unit = { }
Then, we can pass it using syntax like:
run(foo)
Great, but what if we want to use a function that we have defined in the normal way? Or pass a method at all? Well, there is a possibility for that too - a link to a function . This is how we can check a string for emptiness:
str.run(String::isEmpty)
. , , . , . ? ? "" ?
, , , , - :
val parse: (String) -> List<Int> = { it.split(":").map(String::toInt) }
val (xMin, yMin) = parse(data["from"])
val (xMax, yMax) = parse(data["to"])
, , , , , . let
, run
, with
, apply
, also
. ? , .
inline fun <T, R> T.let(block: (T) -> R): R
inline fun <T> T.also(block: (T) -> Unit): T
let
also
. , - block(this)
. , " " . , . also
, let
, .
inline fun <R> run(block: () -> R): R
inline fun <T, R> T.run(block: T.() -> R): R
inline fun <T, R> with(receiver: T, block: T.() -> R): R
inline fun <T> T.apply(block: T.() -> Unit): T
run
, with
apply
:
run
let
, apply also
, with
run
, receiver
. , let
also
? , it
this
, .
? .
, , , , , , . "" .
inline
? , , . , , , , .
. .
, , - ( )?
, , :
let {
val some = Some()
it.run(some::doSome)
}
:
let(Some::doSome)
, , ?
, , ? , . , companion object
:
class Some {
companion object {
fun doSome(any: Any) = run {}
}
}
, .
Factory
:
val other = Other()
val stuff = other.produceStuff()
val some = Some(stuff)
. , Other Some, .
, :
val some = Some(
Other().produceStuff()
)
. , , ... ? , Factory-:
class Some {
companion object Factory {
inline fun <T>create(t: T?, f: (T?) -> Stuff) = Some(f(t))
}
}
:
val some = Some(Other()) { it.doStuff() }
Other :
val some = Some.create(Other) { it.create().doStuff() }
, . ? , . , .
-
, - , . , , . :
fun Some.foo() = run { }
:
val foo: Some.() -> Unit = { }
, - . -. , IntelliJ IDEA , - , .
, -. val
, foo
, . fun
, , .
:
class Some {
fun Other.someExtention() = run { }
}
, , "", - .
, . . . , , - Some Other.
, , , - Some::someExtention
. , - .
P.S.
, , . , KFunction.
fun Some.overlay(f: KFunction1<Some, Any>) = f(this)
In this case, I have two news - the first is that your tastes are very specific, and the second, that in this case our extension function is processed as a very ordinary function, in which the first parameter is an instance of the class that it extends.