title: Smart Cast with hashmap keys
body: <p>I'm porting some Java apps to Kotlin, and I keep running into this issue with hashmaps. Let's say I have two hashmaps:</p>
  <pre><code>var firstHashmap: HashMap&lt;String, String&gt; = hashMapOf(&quot;foo&quot; to &quot;A&quot;, &quot;bar&quot; to &quot;B&quot;)
  var secondHashmap: HashMap&lt;String, String&gt; = hashMapOf(&quot;foo&quot; to &quot;C&quot;, &quot;bar&quot; to &quot;D&quot;)
  </code></pre>
  <p>And I want to update one if the same key exists in the other:</p>
  <pre><code>if (firstHashmap[&quot;foo&quot;] != null) {
      secondHashmap[&quot;foo&quot;] = firstHashmap[&quot;foo&quot;]
  }
  </code></pre>
  <p>Kotlin won't compile the second line because <code>firstHashmap</code> isn't guaranteed to have a &quot;foo&quot; key. It doesn't matter that I just checked it on the first line -- the smart cast system apparently doesn't work with hashmap keys. This has the same issue:</p>
  <pre><code>if (firstHashmap.containsKey(&quot;foo&quot;)) {
      secondHashmap[&quot;foo&quot;] = firstHashmap[&quot;foo&quot;]
  }
  </code></pre>
  <p>This works, but creating the extra variable gets messy in some situations:</p>
  <pre><code>val newValue = firstHashmap[&quot;foo&quot;]
  if (newValue != null) {
      secondHashmap[&quot;foo&quot;] = newValue
  }
  </code></pre>
  <p>This also works, but so far I've never had to use the <code>!!</code> modifier:</p>
  <pre><code>if (firstHashmap[&quot;foo&quot;] != null) {
      secondHashmap[&quot;foo&quot;] = firstHashmap[&quot;foo&quot;]!!
  }
  </code></pre>
  <p>And this works, but the default value after the Elvis operator will never be used, and is just there to satisfy the compiler, which feels wrong:</p>
  <pre><code>if (firstHashmap[&quot;foo&quot;] != null) {
      secondHashmap[&quot;foo&quot;] = firstHashmap[&quot;foo&quot;] ?: &quot;&quot;
  }
  </code></pre>
  <p>Taking that further, I tried dispensing with the conditional and just using the Elvis operator, but then it gets stuck on <code>secondHashMap</code> not having a guaranteed key:</p>
  <pre><code>secondHashmap[&quot;foo&quot;] = firstHashmap[&quot;foo&quot;] ?: secondHashmap[&quot;foo&quot;]
  </code></pre>
  <p>So then this would work, but that brings me back to adding a default value just to trick the compiler:</p>
  <pre><code>secondHashmap[&quot;foo&quot;] = firstHashmap[&quot;foo&quot;] ?: secondHashmap[&quot;foo&quot;] ?: &quot;&quot;
  </code></pre>
  <p>Is there a better way to handle this, like some way to trigger the smart cast system to know that the key is really there? Otherwise I guess the <code>!!</code> option seems cleanest, especially when it's clear from the previous line why I'm choosing to use it.</p>