What are the best practices for scoping when extending (multiple) class(es) with operators? Currently, I have all my extensions as top-level functions across multiple files (loosely sorted by class to be extended) but in the same package. That means, I can simply run import my.extensions.package.*
to get all extensions. However, scoping with with
does not work because I do not have an object/class for scoping. As a minimum working example, I would like to extend the MutableList
class with fun operator minus
to remove an object from the list, extensions live in extensions
package, “production code” lives in otherpackage
. For simplicity, only one extension function is specified, but this generalizes to multiple extension functions:
Extension1.kt
:
package extension
operator fun <T> MutableList<T>.minus(t: T ) = this.let { it.remove(t); it }
I can star import the entire package and then simply run, in Run1.kt
:
package otherpackage
import extension.*
fun run1() = mutableListOf(null as Any?).let { it - null; it }
In order to be able to use scoping, I made a second file with the same extension Extension2.kt
, but this time the method is held in an object
:
package extension
object Extension2 {
operator fun <T> MutableList<T>.minus(t: T ) = this.let { it.remove(t); it }
}
I can now scope using with
, which can be useful to avoid namespace clashes (Run2.kt
):
package otherpackage
import extension.Extension2
fun run2() = with (Extension2) { mutableListOf(null as Any?) - null }
However, star import does not work anymore. While this might seem irrelevant for this example, this becomes rather annoying with more (operator) extension functions:
package otherpackage
// works
import extension.Extension2.minus
// does not work
//import test.Extension2.*
fun run3() = mutableListOf(null as Any?).let { it - null; it }
Is there a way to reconcile the ease of use with the star import and the ability to scope with with
, in particular for (operator) extension functions?
Update: This is probably related/