Kotlin     PHP       WEB       3GL   4GL   5GL      

Kotlin

(extensions)

, C# Gosu, Kotlin . "". , . Kotlin - -.

-

, -, , , . swap MutableList<Int>:

fun MutableList<Int>.swap(index1: Int, index2: Int) {
    val tmp = this[index1] // 'this'    Int
    this[index1] = this[index2]
    this[index2] = tmp
}

this - ( ). MutableList<Int>:

val l = mutableListOf(1, 2, 3)
l.swap(0, 2) // 'this'  'swap()'     'l'

, MutableList<T>, :

fun <T> MutableList<T>.swap(index1: Int, index2: Int) {
    val tmp = this[index1] // 'this'   
    this[index1] = this[index2]
    this[index2] = tmp
}

- , -. . .

, . , , . , .

, : , - , , . :

open class C

class D: C()

fun C.foo() = "c"

fun D.foo() = "d"

fun printFoo(c: C) {
    println(c.foo())
}

printFoo(D())

"" , - c, C .

, - , , . :

class C {
    fun foo() { println("member") }
}

fun C.foo() { println("extension") }

c.foo() c C, "member", "extension".

, - -, , :

class C {
    fun foo() { println("member") }
}

fun C.foo(i: Int) { println("extension") }

C().foo(1) "extension".

null

, null . , null. this == null . toString() Koltin null: -.

fun Any?.toString(): String {
    if (this == null) return "null"
    //    null, `this`    -null ,  toString()
    //  (.: resloves)  -  Any
    return toString()
}

-

, Kotlin :

val <T> List<T>.lastIndex: Int
    get() = size - 1

, - backing field. -. , /.

:

val Foo.bar = 1 // :     -

(.: companion object extensions)

, :

class MyClass {
    companion object { }  //  "ompanion"
}

fun MyClass.Companion.foo() {
    // ...
}

, :

MyClass.foo()

, :

package foo.bar
 
fun Baz.goo() { ... } 

, , , :

package com.example.usage

import foo.bar.goo //      "goo"
                   // 
import foo.bar.*   //    "foo.bar"

fun usage(baz: Baz) {
    baz.goo()
)

. Imports .

. (.:implicit receivers), . , , (.: dispatch receiver), , (.: extension receiver).

class D {
    fun bar() { ... }
}

class C {
    fun baz() { ... }

    fun D.foo() {
        bar()   //  D.bar
        baz()   //  C.baz
    }

    fun caller(d: D) {
        d.foo()   //  -
    }
}

, , , , , .

class C {
    fun D.foo() {
        toString()         //  D.toString()
        this@C.toString()  //  C.toString()
    }

, , open . , , , - , .

open class D {
}

class D1 : D() {
}

open class C {
    open fun D.foo() {
        println("D.foo in C")
    }

    open fun D1.foo() {
        println("D1.foo in C")
    }

    fun caller(d: D) {
        d.foo()   //  -
    }
}

class C1 : C() {
    override fun D.foo() {
        println("D.foo in C1")
    }

    override fun D1.foo() {
        println("D1.foo in C1")
    }
}

C().caller(D())   // prints "D.foo in C"
C1().caller(D())  // prints "D.foo in C1" -    
C().caller(D1())  // prints "D.foo in C" -    

Java "*Utils": FileUtils, StringUtils .. java.util.Collections. - :

// Java
Collections.swap(list, Collections.binarySearch(list, Collections.max(otherList)), Collections.max(list))

. - :

// Java
swap(list, binarySearch(list, max(otherList)), max(list))

, IDE, , - . , :

// Java
list.swap(list.binarySearch(otherList.max()), list.max())

List, ? .

Kotlin     PHP       WEB       3GL   4GL   5GL         w  

, , Collaboration diagrams - - , , . , , -. , .




 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 -> - _.
Bourabai Research -  XXI Bourabai Research Institution