Is there a way to add a "scoped" extension to clas...
# announcements
b
Is there a way to add a "scoped" extension to class A inside of class B, outside of class B? For example, I tried this but it complains:
Copy code
class Foo {
   fun String.someStringExtension() { ... }
}

fun Foo.String.someOtherStringExtension() { ... }
a
I think no. Here is the issue for tracking: https://youtrack.jetbrains.com/issue/KT-10468
b
Thanks for the link 👍
u
You could do something like this:
Copy code
class Scope {
    fun String.appendSomething() = this + "something"
}

fun scopedExtensions(body : Scope.() -> Unit) {
    body.invoke(Scope())
}

fun test() {
    scopedExtensions {
        "Test".appendSomething() // Compiler is ok with this
    }
    "Bla".appendSomething() // Compiler complains here
}