Le monde devient fou. Ils disent que tous les nouveaux projets mobiles sur Android sont Ă©crits exclusivement en Kotlin. De nos jours, il est trĂšs dangereux de ne pas apprendre les nouvelles technologies. Au dĂ©but, vos connaissances deviennent obsolĂštes, vous quittez le travail, vivez prĂšs d'une canalisation de chauffage, combattez avec des sans-abri pour la nourriture et mourez dans l'obscuritĂ©, sans avoir appris la programmation fonctionnelle. Je suis donc allĂ© Ă Kurser pour Ă©tudier le cours Kotlin for Java Developers et j'ai commencĂ© Ă lire un livre (salut, abreslav , yole ), j'ai demandĂ© Ă mes amis oĂč vous en Ă©tiez et je suis revenu avec un certain vide dans mon Ăąme. Aidez Oleg le voyageur Ă trouver un sens Ă Kotlin!
- Bonus: habro-question "Comment utilisez-vous 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, , . , , , , . .