title: List elements matching on their type
body: <p>I've got a code similar to one below:</p>
  
  <pre><code>def walkTree(list:List[Command]) {
      list match {
          case Command1::rest =&gt; doSomething(); walkTree(rest)
          case Command2::rest =&gt; doSomethingElse(); walkTree(rest)
          case Nil =&gt; ;
      }
  }
  </code></pre>
  
  <p>I also know that you can pattern match on specific type and assign a variable at the same time:</p>
  
  <pre><code>try {
      ...
  }
  catch {
      case ioExc:IOException =&gt; ioExc.printStackTrace()
      case exc:Exception =&gt; throw new RuntimeException("Oh Noes", e);
  }
  </code></pre>
  
  <p>Is there a way to combine both in something like below:</p>
  
  <pre><code>def walkTree(list:List[Command]) {
      list match {
          case cmd1:Command1::rest =&gt; doSomething(); walkTree(rest)
          case cmd2:Command2::rest =&gt; doSomethingElse(); walkTree(rest)
          case Nil =&gt; ;
      }
  }
  </code></pre>
  
  <p>Or do I need to extract each list element before matching?</p>