Where to declare Kotlin extension functions in an Android app
Asked Answered
S

4

12

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?

Sextodecimo answered 20/2, 2019 at 10:51 Comment(3)
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? You can actually test this out yourself. I personally group extensions by namespace for better organisationMosher
You can declare wherever you want in your project. Personally I distribute the extension by grouping them in meaningful named classes within an extensions packageKetchan
Actually, it doesn't matter to where you writing them. The extension function are mapped with a class which is already a part of your application. So, it's up to you in which you are going write you extension functions.Coxcombry
A
17

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
Autograft answered 20/2, 2019 at 10:59 Comment(1)
Please be aware of Daniel Selvans answer - Using the extensions over the project-scope requires importing it (import main_package_name.util.*).Ulotrichous
A
4

They can be anywhere, but it seems to make sense to have extensions for a particular class in the same file and/or package. For example, extensions to String could be in StringExtensions.kt, and that could optionally be in an extensions package.

Azimuth answered 20/2, 2019 at 11:0 Comment(0)
B
4

For a extension to be global, you need to put it outside of class

package com.extension.globalcontext

fun <T> MutableList<T>.swap(index1: Int, index2: Int)

And to call them you need to import the package

import com.extension.globalcontext
l.swap(0, 2)
Bulimia answered 20/2, 2019 at 11:6 Comment(0)
P
1

Yes, extention functions can be declared anywhere you want.

For functions of general utilities (regarding lists), I whould place them under something like /company/app/util/lists.kt.

Panoply answered 20/2, 2019 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.