I ran into the same problem. My solution is basically the same as yours, with a little clarification:
inline fun <T> T.apply(f: T.() -> Any): T { this.f() return this }
Note that f is an extension function. This way you can reference the methods on your object using the implicit this link. Here is an example taken from my libGDX project:
val sprite : Sprite = atlas.createSprite("foo") apply { setSize(SIZE, SIZE) setOrigin(SIZE / 2, SIZE / 2) }
Of course, you can also call doStuff(this) .
Kirill Rakhman
source share