title: How to pattern match head and tail types of a scala list?
body: <p>I would like to <code>pattern match</code> on different segments of a <code>list</code> in <code>scala</code> on the types of the <code>head</code> and <code>tail</code>:</p>
  
  <pre><code>class Solution07 extends FlatSpec with ShouldMatchers {
    "plain recursive flatten" should "flatten a list" in {
      val list1 = List(List(1, 1), 2, List(3, List(5, 8)))
      val list1Flattened = List(1, 1, 2, 3, 5, 8)
  
      flattenRecur(list1) should be (list1Flattened)
    }
  
    def flattenRecur(ls: List[Any]): List[Int] = ls match {
      case (head: Int) :: (tail: List[Any]) =&gt; head :: flattenRecur(tail)
      case (head: List[Int]) :: (tail: List[Any]) =&gt; head.head :: flattenRecur(head.tail :: tail)
      case (head: List[Any]) :: (tail: List[Any]) =&gt; flattenRecur(head) :: flattenRecur(tail) // non-variable type... on this line.
    }
  }
  </code></pre>
  
  <p>I get:</p>
  
  <blockquote>
    <p>Error:(18, 17) non-variable type argument Int in type pattern
    List[Int] (the underlying of List[Int]) is unchecked since it is
    eliminated by erasure
        case (head: List[Int]) :: (tail: List[Any]) => head.head :: flattenRecur(head.tail :: tail)
                    ^</p>
  </blockquote>
  
  <p>What am I missing? how is it possible for me to pattern match on the <code>head</code> and <code>tail</code>'s <strong>types</strong> of the list?</p>