Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to declare Kotlin extension functions in an Android app

Suppose I have the following code that I want to make as a re-usable component:

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

and I want to use it anywhere in my app as follows:

val l = mutableListOf(1, 2, 3)
l.swap(0, 2)

Correct me if I'm wrong but I believe that function extension declarations can exist outside of a class. So in an Android app, where would I put this declaration? Or does it even matter? Will the compile just compile the code regardless where the extension is declared and make it re-usable globally or do I have to make it part of a class?

like image 410
Johann Avatar asked Feb 20 '19 10:02

Johann


People also ask

How do I create a Kotlin extension property?

Kotlin allows to define Extension Functions with a nullable class type. These extension function can be called on a nullable object variable. To define an extension for Nullable receiver, we just need to add a check for null receiver inside the extension function, and the appropriate value is returned.

How do extension functions work in Kotlin?

Extension functions are a cool Kotlin feature that help you develop Android apps. They provide the ability to add new functionality to classes without having to inherit from them or to use design patterns like Decorator.

Can we write extension function inside a class body?

This is done via special declarations called extensions. For example, you can write new functions for a class or an interface from a third-party library that you can't modify. Such functions can be called in the usual way, as if they were methods of the original class. This mechanism is called an extension function.

How to add extension function to a class in Kotlin?

When a function is added to an existing class it is known as Extension Function. To add an extension function to a class, define a new function appended to the classname as shown in the following example: package kotlin1.com.programmingKotlin.chapter1 class Circle (val radius: Double) {

How do I use Kotlin in Android Studio?

In fact, if you place your cursor inside the toast (“Button Clicked!”) line of code, and then select ‘Tools > Kotlin > Show Kotlin Bytecode’ from the Android Studio toolbar, you’ll see this static method being invoked. You can even use this extension function in a Java class by importing it at the call site:

How to create a class name in Kotlin?

To create a name for this class, the compiler takes the corresponding Kotlin source file (extensions), capitalizes it (Extensions), and adds ‘Kt.’

How to extend a class with new functionality in koltin?

However, in Koltin, you can also use extension function to extend a class with new functionality. Basically, an extension function is a member function of a class that is defined outside the class. For example, you need to use a method to the String class that returns a new string with first and last character removed;


1 Answers

You can create some file, e.g. ListExt.kt anywhere you want (for example in package main_package_name/util/ListExt.kt) and place the extension function there and it will be available in the whole project. So the content of the ListExt.kt file can be:

package main_package_name.util

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

// other extension functions on the Lists
like image 87
Sergey Avatar answered Sep 25 '22 23:09

Sergey