Há um desenvolvedor móvel na floresta, vê - Kotlin está pegando fogo. Ele se sentou em Kotlin e queimou

O mundo está ficando louco. Eles dizem que todos os novos projetos móveis no Android são escritos exclusivamente no Kotlin. Hoje em dia é muito perigoso não aprender novas tecnologias. A princípio, seu conhecimento se torna obsoleto, você sai do trabalho, mora perto de uma estação de aquecimento, luta com moradores de rua por comida e morre na obscuridade, sem ter aprendido programação funcional. Então, fui ao Kurser para estudar o curso Kotlin for Java Developers e comecei a ler um livro (oi, abreslav , yole ), pedi aos meus amigos o seu próprio conhecimento de onde e voltei com um certo vazio na minha alma. Ajude Oleg, o viajante a encontrar significado em Kotlin!


  • BĂ´nus: habro-question "Como vocĂŞ usa o Kotlin?"



â—Ź  Java , . a = b — , a[1] = 2 — . - . IDE . IDE , , .


â—Ź  API , - map/filter , . . , IDE — .


â—Ź  , IDE. Kotlin IntelliJ IDEA? , Java? . , - JB .


â—Ź  it, . - seq.map { it -> foo(it, 1); }.map { it -> bar(it, 2); }.filter { it -> it.getBaz() > 0; }. ? ! « , , , ».



â—Ź  ?.let { foo(it); }?.let { bar(it); } — . , . if. .


â—Ź  . JvmStatic JvmName, .


, :


class C {
    companion object {
        @JvmStatic fun foo() {}
        fun bar() {}
    }
}

, . :


  • C.foo(); —
  • C.bar(); — ,
  • C.Companion.foo(); —
  • C.Companion.bar(); —

? , . , , , :


fun List<String>.filterValid(): List<String>
fun List<Int>.filterValid(): List<Int>

JVM : filterValid(Ljava/util/List;)Ljava/util/List;


:


fun List<String>.filterValid(): List<String>

@JvmName("filterValidInt")
fun List<Int>.filterValid(): List<Int>

: Kotlin checked exceptions. Java- . « » @Throws:


@Throws(IOException::class)
fun foo() {
    throw IOException()
}

, « , ». , ?


, Java-to-Kotlin Interop , .


â—Ź  / get (, ENGLISH? -) — .


import java.util.Calendar
fun calendarDemo() {
    val calendar = Calendar.getInstance()
    if (calendar.firstDayOfWeek == Calendar.SUNDAY) {  // call getFirstDayOfWeek()
        calendar.firstDayOfWeek = Calendar.MONDAY      // call setFirstDayOfWeek()
    }
    if (!calendar.isLenient) {                         // call isLenient()
        calendar.isLenient = true                      // call setLenient()
    }
}

â—Ź  - , .


, . , « », — . MutableList swap:


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

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

- , , , . - . , : , , ? -, ?


, - . , , -.


â—Ź  . , reduce.


reduce:


listOf(1, 2, 3).reduce { sum, element -> sum + element } == 6

identity (fold), .


listOf(1, 2, 3).fold(0) { sum, element -> sum + element } == 6

, -? , .


, fold reduce , fold , reduce . , identity .


? - Optional , ? null , null-friendly .


â—Ź  . , -, ? .


, - :


data class User(val name: String, val age: Int)
val duncan = User("Duncan MacLeod", 426) 
val (name, age) = duncan
println("$name, $age years of age") //  "JaDuncan MacLeodne, 426 years of age"

:


val (name, age) = Pair("Java", 23)
println("$name, $age years of age") //   "Java, 23 years of age"

, :


public data class Pair<out A, out B>(
    public val first: A,
    public val second: B
)

, , . , - . , , .


â—Ź  — ( ).


C++, . , — . , C++ return , - . , undefined behavior. , . — . , . .


, Kotlin . , . a b, c, when, d, e f, !


fun a(check: Int) = b(check)
fun b(check: Int) = c(check)

fun c(check: Int) =
    when (check) {
        1 -> d()
        2 -> e()
        else -> f()
    }

fun d() = "result 1";
fun e() = "result 2";
fun f() = "result 3";

fun main(args: Array<String>) {
    println(::a.returnType)
    for (i in 1..3)  println(a(i).javaClass.name)

, , . f, , , .


:


kotlin.String
java.lang.String
java.lang.String
java.lang.String

:


fun d() = "1";
fun e() = 100500;
fun f() = listOf<String>();


kotlin.Any
java.lang.String
java.lang.Integer
kotlin.collections.EmptyList

API. API , Kotlin .



, . , , , . , Kotlin- :-)


Joker 2018 ( ), (asm0dey) , Kotlin ( ), , GraalVM, Spring, Spring Security, Spring Transactions, jOOQ, ..


Kotlin Java ? . , Kotlin . !


. , 8-9 2018, Mobius. JetBrains , Kotlin Muplitplatform. , , Kotlin, , . , , , , . .

Source: https://habr.com/ru/post/pt431678/


All Articles