Kotlin’s crossinline keyword

Problem: non-local return Let’s first understand the problem of non-local return with a simple example: fun doSomething() { println(“Before lambda”) doSomethingElse { println(“Inside lambda”) return // This is non-local return } println(“After lambda”) } inline fun doSomethingElse(lambda: () -> Unit) { println(“Do something else”) lambda() } Non-local return In the code above, the return statement … Read more

Button onClick attribute is none if activity written in Kotlin

It seems like the designer does not support Kotlin yet. Here are some solution: XML (Not Recommended) Add the following line to your Button tag. This is exactly what the designer will do. android:onClick=”sendMessage” Old Fashion No need to add anything. val button = findViewById<Button>(R.id.Button) button.setOnClickListener { } kotlin-android-extensions (Recommended) Add apply plugin: “kotlin-android-extensions” to … Read more

How do I create a map from 2 arrays?

You can zip together the arrays to get a list of pairs (List<Pair<String, Int>>), and then use toMap to get your map. Like this: val keys = arrayOf(“butter”, “milk”, “apples”) val values = arrayOf(5, 10, 42) val map: Map<String, Int> = keys.zip(values) // Gives you [(“butter”, 5), (“milk”, 10), (“apples”, 42)] .toMap() // This is … Read more

When to use Kotlin suspend keyword?

As a general rule of thumb, you should only declare your function suspend if the compiler forces you to. One possible exception to this rule would be if you’re defining this function as an open method (for instance in an interface) and you expect that some implementations/overrides will need to call suspending functions themselves. It’s … Read more

Implicitly map to set in Kotlin

Looking at the library code, the only other way to do this would be to call mapTo which takes a destination collection: oldSet.mapTo(HashSet<String>()) { getNewStringFromOld(id) } I’m not sure the default is wrong. The problem is that map might be used in a way that results in a list of non-unique values. If there was … Read more

Compilation error: Smart cast to ” is impossible, because ” is a local variable that is captured by a changing closure

In general, when a mutable variable is captured in a lambda function closure, smart casts are not applicable to that variable, both inside the lambda and in the declaring scope after the lambda was created. It’s because the function may escape from its enclosing scope and may be executed later in a different context, possibly … Read more

When exactly is the operator keyword required in Kotlin?

Why does this code compile? This compiles because the overridden interface method, Comparable<T>.compareTo, is itself an operator fun. /** * Compares this object with the specified object for order. Returns zero if this object is equal * to the specified [other] object, a negative number if it’s less than [other], or a positive number * … Read more

How to sort objects list in case insensitive order?

It looks like compareBy might be able to take a Comparator as an argument, see the documentation here: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.comparisons/compare-by.html Try: places.sortWith(compareBy(String.CASE_INSENSITIVE_ORDER, { it.name })) or places.sortWith(compareBy(String.CASE_INSENSITIVE_ORDER, Place::name)) to sort the list in place, or you can assign it to a new variable using val newList = places.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER, { it.name }))

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)