Kotlin List tail function

Kotlin doesn’t have a built-in List<T>.tail() function, so implementing your own extension function is the only way. Although your implementation is perfectly fine, it can be simplified a bit: fun <T> List<T>.tail() = drop(1) Or, instead of extension function, you can define an extension property: val <T> List<T>.tail: List<T> get() = drop(1) val <T> List<T>.head: … Read more

Kotlin Extension Functions suddenly require api level 24

What you are using is not kotlin.collections.MutableMap.forEach. What you are using seems to be Map.forEach in Java 8. Refer to this article: http://blog.danlew.net/2017/03/16/kotlin-puzzler-whose-line-is-it-anyways/ This seems to be a common mistake. Java 8 is well-supported from Android API level 24. For short, do not use forEach like map.forEach { t, u -> Log.i(“tag”, t + u) … Read more

Deep copy of list with objects in Kotlin

That’s bacause you are adding all the object references to another list, hence you are not making a proper copy, you have the same elements in two list. If you want diferents list and diferent references, you must clone every object in a new list: public data class Person(var n: String) fun main(args: Array<String>) { … Read more

Kotlin extension function access Java private field

First, you need to obtain a Field and enable it can be accessible in Kotlin, for example: val field = ABC::class.java.getDeclaredField(“mPrivateField”) field.isAccessible = true Then, you can read the field value as Int by Field#getInt from the instance of the declaring class, for example: val it: ABC = TODO() val value = field.getInt(it) Last, your … Read more

Kotlin Android debounce

I’ve created a gist with three debounce operators inspired by this elegant solution from Patrick where I added two more similar cases: throttleFirst and throttleLatest. Both of these are very similar to their RxJava analogues (throttleFirst, throttleLatest). throttleLatest works similar to debounce but it operates on time intervals and returns the latest data for each … Read more

How to extend a data class with toString

Adding a .toString() extension function would not work because: Extension functions can’t take part in virtual calls (they are resolved statically). In other words, extensions cannot override member functions. If there is a matching member function, it is preferred to the extension. If you add an extension function fun Something.toString() = …, then s.toString() won’t … Read more

throws Exception in a method with Kotlin

In Kotlin, there’s no checked exceptions, no exceptions have to be declared and you aren’t forced to catch any exception, though, of course, you can. Even when deriving from a Java class, you don’t have to declare exceptions that a method throws. @Throws(SomeException::class) is just intended for Java interoperability, which allows one to write a … Read more

Accidental override: The following declarations have the same JVM signature

This happens because the Kotlin compiler tries to generate a getter for val context declared in your class primary constructor, namely a method getContext(), but the base class ArrayAdapter<T> already has such a method. You can solve that by doing one of the following: Change your class’ constructor parameter not to be a val. class … Read more