title: Kotlin - Restrict extension method scope
body: <p>Is there a way to restrict extension methods in DSLs?</p>
  
  <p>Say I have a class structure like this:</p>
  
  <pre><code>class Outer {
      fun middle(op: Middle.() -&gt; Unit): Middle {
          val middle = Middle()
          middle.op()
          return middle
      }
  }
  
  class Middle {
      fun inner(op: Inner.() -&gt; Unit): Inner {
          val inner = Inner()
          inner.op()
          return inner
      }
  }
  
  class Inner
  
  fun outer(op: Outer.() -&gt; Unit): Outer {
      val outer = Outer()
      outer.op()
      return outer
  }
  </code></pre>
  
  <p>I can then create a call like so:</p>
  
  <pre><code>outer {
      middle {
          inner {
              middle { }  // Here is the problem
          }
      }
  }
  </code></pre>
  
  <p>My problem is that the marked <code>middle { }</code> call is confusing, as it adds a <code>Middle</code> to the <code>Outer</code> when it looks like it is adding to the <code>Inner</code>.</p>
  
  <p>Is there a way to not allow the <code>middle { }</code> call?</p>