Functions with 2 receivers (a builder and a companion object)

I have a lot of fluent builder functions that are implemented in a companion object (to avoid polluting namespaces) I would like to provide a nice builder api, so I need to be able to call these functions, by chaining tem, without specifying the companion object each time I use one At the moment, the best I managed to do is to code it like this :  

Is there a way to code the following function
{code}

 
fun A.myFunction(b: B.Companion.() -> A.() -> Unit): Unit = with (this) { B.Companion.b() }()

{code}
But with this syntax, I need to use 2 pairs of brackets like this :
{code}
a.myFunction {{
  doSomething()
}}
{code}

I would like to achieve the same effect than this
{code}
with (a) {
  with(B.Companion) {
           doSomething
           }
}
{code}

But with a single pair of brackets
{code}
a.myFunction {
  doSomething()
}
{code}

Is there a way to achieve this ?

1 Like

a possibility would be to turn my builder functions into top level functions of a sub-package -> I would be able to import them all with a * BUT I don't want to create gazillon of packages for my (generated) classes

It looks like we can’t import all functions from a companion object with *
-> Extension functions in a companion object are a bit painfull to use though you can use them by using “with”, you can’t hide that behind a nice function

with(CompanionObject) {
  “”.builderFunction(“gah”)  // ok
}

but I still don’t know how to make that work :

myNiceFunction {
“”.builderFunction(“gah”)   // not ok…can’t find the right signature for myNiceFunction to make that work
}