Kotlin fun
fun double(x: Int): Int {
}
val result = double(2)
Sample().foo() // Sample foo
, :
// Int
infix fun Int.shl(x: Int): Int {
...
}
// , infix
1 shl 2
// ,
1.shl(2)
Pascal, :. . .
fun powerOf(number: Int, exponent: Int) {
...
}
, , . .
fun read(b: Array<Byte>, off: Int = 0, len: Int = b.size()) {
...
}
= .
, . , :
open class A {
open fun foo(i: Int = 10) { ... }
}
class B : A() {
override fun foo(i: Int) { ... } //
}
. , , .
fun reformat(str: String,
normalizeCase: Boolean = true,
upperCaseFirstLetter: Boolean = true,
divideByCamelHumps: Boolean = false,
wordSeparator: Char = ' ') {
...
}
,
reformat(str)
, , -
reformat(str, true, true, false, '_')
reformat(str,
normalizeCase = true,
upperCaseFirstLetter = true,
divideByCamelHumps = false,
wordSeparator = '_'
)
,
reformat(str, wordSeparator = '_')
, Java , - Java .
Unit
, - Unit
. Unit
- - Unit
.
fun printHello(name: String?): Unit {
if (name != null)
println("Hello ${name}")
else
println("Hi there!")
// `return Unit` `return`
}
Unit
. , ,
fun printHello(name: String?) {
...
}
- , { }
=
fun double(x: Int): Int = x * 2
. , .
fun double(x: Int) = x * 2
, , ( Unit
).
Kotlin , ( ).
( ) vararg
:
fun <T> asList(vararg ts: T): List<T> {
val result = ArrayList<T>()
for (t in ts) // ts - (Array)
result.add(t)
return result
}
, , :
val list = asList(1, 2, 3)
vararg
T
T
, ts
Array<out T>
.
vararg
. vararg
,
named argument , , , .
vararg
, --, asList(1, 2, 3)
, , , spread ( *
):
val a = arrayOf(1, 2, 3)
val list = asList(-1, 0, *a, 4)
Kotlin . , - , ( Java, C# Scala). , Kotlin , - (. "member functions") - ("extension functions").
Koltin . , ,
fun dfs(graph: Graph) {
fun dfs(current: Vertex, visited: Set<Vertex>) {
if (!visited.add(current)) return
for (v in current.neighbors)
dfs(v, visited)
}
dfs(graph.vertices[0], HashSet())
}
( closure). , , , visited .
fun dfs(graph: Graph) {
val visited = HashSet<Vertex>()
fun dfs(current: Vertex) {
if (!visited.add(current)) return
for (v in current.neighbors)
dfs(v)
}
dfs(graph.vertices[0])
}
- - ,
class Sample() {
fun foo() { print("Foo") }
}
-
Sample().foo() // Sample foo
- (Generic Functions)
, .
fun <T> singletonList(item: T): List<T> {
// ...
}
Kotlin , " ".
, .
tailrec
, , , .
tailrec fun findFixPoint(x: Double = 1.0): Double
= if (x == Math.cos(x)) x else findFixPoint(Math.cos(x))
fixpoint
, . Math.cos, 1.0 , , 0.7390851332151607. :
private fun findFixPoint(): Double {
var x = 1.0
while (true) {
val y = Math.cos(x)
if (x == y) return y
x = y
}
}
tailrec
, , . , - . try/catch/finally. , backend Java(JVM).
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |