title: Compilation error: Smart cast to '<type>' is impossible, because '<variable>' is a local variable that is captured by a changing closure
body: <p>To simplify my real use case, let's suppose that I want to find the maximum number in a list:</p>
  
  <pre><code>var max : Int? = null
  listOf(1, 2, 3).forEach {
      if (max == null || it &gt; max) {
          max = it
      }
  }
  </code></pre>
  
  <p>However, compilation fails with the following error:</p>
  
  <blockquote>
    <p>Smart cast to 'Int' is impossible, because 'max' is a local variable that is captured by a changing closure</p>
  </blockquote>
  
  <p>Why does a changing closure prevent smart cast from working in this example?</p>