title: Pattern matching in anonymous function in Scala
body: <p>I'm a beginner Scala developer having problems with a coding exercise (5.12) taken by the book "Functional Programming in Scala" by Paul Chiusano.</p>
  
  <p>I have this function here, called <em>unfold</em>, that takes an initial state and a function for producing a stream with the next states:</p>
  
  <pre><code>def unfold[A,S](z: S)(f: S =&gt; Option[(A,S)]): Stream[A] = f(z) match {
    case Some((h,t)) =&gt; h #:: unfold(t)(f)
    case _ =&gt; Stream.empty
  } 
  </code></pre>
  
  <p>For example, with this function one can create an infinite stream of objects, e.g. </p>
  
  <p><code>def constant[A](a: A): Stream[A] = unfold(a)(_ =&gt; Some(a,a))</code></p>
  
  <p>Now, I want to create the Fibonacci sequence, then I type:</p>
  
  <p><code>def fibs: Stream[Int] = unfold((0,1))((a,b) =&gt; Some(a,(b,a+b)))</code></p>
  
  <p>I get these errors: </p>
  
  <ul>
  <li><p>Missing parameter type b </p></li>
  <li><p>Expression of type Some[((Int,Int),(Nothing,String))] doesn't conform to expected type Option[(A_,(Int,Int))]</p></li>
  </ul>
  
  <p>If I use the <em>case</em> keyword inside the anonymous function passed to <em>unfold</em>, like: </p>
  
  <p><code>{ case (a,b) =&gt; Some(a,(b,a+b))}</code> </p>
  
  <p>everything's fine. </p>
  
  <p>So my question is: what's the difference between the two implementations? Is it something about type inference that I don't understand?</p>