Short Circuits, Bottom Types, and the Vacuous Boomerang
<p>It’s a real program, I promise. It compiles and runs. <a href="https://pl.kotl.in/unRa64z1g" rel="noopener ugc nofollow" target="_blank">Try it!</a></p>
<p>It works because <code>throw return</code> is an expression of type <code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-nothing.html" rel="noopener ugc nofollow" target="_blank">Nothing</a></code>. Normally, an expression is a piece of code that produces a value. When you write something like <code>val x = 1 + 1</code>, you’re saying “evaluate the expression on the right, and then assign the result to the variable on the left.” But when an expression’s return type is <code>Nothing</code>, it means the expression never completes its execution at all. Instead, the program exits that line of code entirely, and skips ahead to somewhere else in the program.</p>
<p>The <code>throw</code> keyword is a good example of an instruction that can cause a program to skip ahead. When you throw an exception, the program will jump right to the next applicable <code>catch</code> or <code>finally</code> section. Or, if the error isn’t caught, the program or thread will crash. Either way, the lines of code after the <code>throw</code> won’t get executed.</p>
<p><a href="https://betterprogramming.pub/short-circuits-bottom-types-and-the-vacuous-boomerang-2114bca82b3f"><strong>Click Here</strong></a></p>
<p> </p>