https://kotlinlang.org logo
#announcements
Title
# announcements
j

jmfayard

08/25/2019, 7:58 AM
Given a DSL that is defined by an external project (Gradle in my case), is it possible to define an extension function
fun String.someExtensionFunction() = "Hello World"
that would be usable only from the right extension block?
Copy code
dependencies { // this is Dependencies
    ....
}
That seems impossible because it would require to define the function having two receivers,
Dependencies
and
String
d

Dico

08/25/2019, 8:05 AM
You can wrap the dependencies with your own class, using interface delegation. Then add your function to the class.
class MyReceiver(deps: Dependencies) : Dependencies by deps) { fun String.function()... } Then create your own dependencies {} function with a slightly different name
j

jmfayard

08/25/2019, 8:08 AM
That's a clever trick
k

karelpeeters

08/25/2019, 9:09 AM
2 Views