class Foo { operator fun invoke(fooBlock: Foo.() -> Unit) = this.apply(fooBlock) operator fun String.invoke(barBlock: Bar.() -> Unit) = Bar().apply(barBlock) fun sayFoo() { println("I'm a Foo!") } } class Bar { operator fun invoke(barBlock: Bar.() -> Unit) = this.apply(barBlock) fun sayBar() { println("I'm a Bar!") } } fun main() { val foo = Foo() foo { sayFoo() "and there should be a bar block" { sayBar() "this shouldn't be allowed I think" { sayFoo() sayBar() } } } }