title: In Kotlin, how can you limit the choices in a fluent Builder for different forks of settings
body: <p>In Kotlin, I am writing a builder and want a series of steps that are obvious and must be completed.  With a fluent builder I can present all steps but not really set the order they must occur, nor can I change which ones are available based on the previous step.  So:</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>serverBuilder().withHost(&quot;localhost&quot;)
           .withPort(8080)
           .withContext(&quot;/something&quot;)
           .build()
  </code></pre>
  <p>is fine, but then adding options like SSL certs:</p>
  <pre class="lang-kotlin prettyprint-override"><code>serverBuilder().withHost(&quot;localhost&quot;)
           .withSsl()
           .withKeystore(&quot;mystore.kstore&quot;)
           .withContext(&quot;/secured&quot;)
           .build()
  </code></pre>
  <p>Now nothing prevents the non-ssl version from having the <code>withKeystore</code> and other options.  There should be an error when calling this SSL method without first turning on <code>withSsl()</code>:</p>
  <pre class="lang-kotlin prettyprint-override"><code>serverBuilder().withHost(&quot;localhost&quot;)
           .withPort(8080)
           .withContext(&quot;/something&quot;)
           .withKeystore(&quot;mystore.kstore&quot;)   &lt;------ SHOULD BE ERROR!
           .build()
  </code></pre>
  <p>And it might be more complicated with more forks in the road where I only want some objects present and not others.</p>
  <p>How do I limit which functions are available at each fork in the builder logic?  Is this impossible for a builder, and instead should be a DSL?</p>
  <p><strong>Note:</strong> <em>this question is intentionally written and answered by the author (<a href="https://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/">Self-Answered Questions</a>), so that the idiomatic answers to commonly asked Kotlin topics are present in SO.</em></p>