title: Is there a way in Kotlin to combine an inline function and a when expression with an extension function?
body: <p>I would like to reduce the size of an if-elseif-else ladder but can't use just <code>when</code> because the smartcast doesn't catch the cases I'm dealing with. I'm wondering if I can combine something like a <code>let</code> or an <code>also</code> and a <code>when</code> into a single inline function to solve this.</p>
  
  <p>I've tried using smart casting with <code>is</code> but it's problematic because I'd have to do a <code>when</code> within the outer <code>when</code> to actually get the result I wanted. I ended up doing something similar to one of the responses from this post: <a href="https://stackoverflow.com/questions/45877140/kotlin-and-idiomatic-way-to-write-if-not-null-else-based-around-mutable-v">Kotlin and idiomatic way to write, &#39;if not null, else...&#39; based around mutable value</a> by just having a <code>let</code> block acting on the non-null variable then executing the <code>when</code> block inside that <code>let</code>.</p>
  
  <p>Case I'm currently going with:</p>
  
  <pre><code>variable?.let { safeVariable -&gt;
      when {
          case1 -&gt; doSomething(safeVariable)
          case2 -&gt; doSomethingElse(safeVariable)
           ...
          else -&gt; catchAll(safeVariable)
      }
      return@let
  }
  Log.e(TAG, "variable was null")
  </code></pre>
  
  <p>Cases I've considered:</p>
  
  <pre><code>when(variable) {
      is Type -&gt; when {
                     case1 -&gt; doSomething(variable)
                     case2 -&gt; doSomethingElse(variable)
                     ...
                     else -&gt; catchAll(variable)
                 }
     else -&gt; Log.e(TAG, "variable was null")
  }
  </code></pre>
  
  <pre><code>if (variable is Type) {
      when {
          case1 -&gt; doSomething(variable)
          case2 -&gt; doSomethingElse(variable)
           ...
          else -&gt; catchAll(variable)
      }
  } else {
      Log.e(TAG, "variable was null")
  }
  </code></pre>
  
  <p>What I'd like to be able to write would look like this:</p>
  
  <pre><code>variable?.alsoWhen { safeVariable -&gt;
      case1 -&gt; doSomething(safeVariable)
      case2 -&gt; doSomethingElse(safeVariable)
       ...
      else -&gt; catchAll(safeVariable)
  } ?: Log.e(TAG, "variable was null")
  </code></pre>
  
  <p>Is this possible to write in Kotlin with an extension function and if not is there at least an alternative to what I wrote above to make it more compact and readable?</p>
  
  <p>EDIT:</p>
  
  <p>Based on Damon's comment below I did think of a slightly cleaner way to approach this with:</p>
  
  <pre><code>when(true) { 
      variable?.case1 -&gt; doSomething(variable) 
      variable?.case2 -&gt; doSomethingElse(variable) 
       ... 
      variable is String -&gt; catchAll(variable)
      else -&gt; Log.e(TAG, "variable was null") 
  } 
  </code></pre>
  
  <p>It would be nice to get rid of the <code>(true)</code> next to the when if possible but this is clean enough that I'm pretty happy with the solution.</p>