El mundo se está volviendo loco. Dicen que todos los nuevos proyectos móviles en Android están escritos exclusivamente en Kotlin. Hoy en día es muy peligroso no aprender nuevas tecnologías. Al principio, su conocimiento se vuelve obsoleto, vuela sin trabajo, vive cerca de una calefacción principal, lucha con personas sin hogar por comida y muere en la oscuridad, sin haber aprendido la programación funcional. Así que fui a Kurser para estudiar el curso Kotlin para desarrolladores de Java y comencé a leer un libro (hola, abreslav , yole ), les pregunté a mis amigos por su propio conocimiento de dónde y regresé con un cierto vacío en mi alma. ¡Ayuda a Oleg, el viajero, a encontrar significado en Kotlin!
- Bonus: habro-question "¿Cómo usas 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, , . , , , , . .