11

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?

Sergio
  • 27,326
  • 8
  • 128
  • 149
Johann
  • 27,536
  • 39
  • 165
  • 279
  • 2
    `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 organisation – Zun Feb 20 '19 at 10:58
  • You can declare wherever you want in your project. Personally I distribute the extension by grouping them in meaningful named classes within an extensions package – Ricardo Feb 20 '19 at 10:58
  • 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. – Guru Karthi R Feb 20 '19 at 11:12

4 Answers4

16

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
Sergio
  • 27,326
  • 8
  • 128
  • 149
  • 1
    Please be aware of Daniel Selvans answer - Using the extensions over the project-scope requires importing it (`import main_package_name.util.*`). – Markus Apr 02 '21 at 14:55
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.

Nelson Wright
  • 506
  • 9
  • 18
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)
Daniel Selvan
  • 959
  • 10
  • 23
0

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.

noiaverbale
  • 1,550
  • 13
  • 27