title: Scala compiler errors when applying an optional transformation function to an optional value
body: <p>I have a list of values in Scala of various types - strings, ints, doubles mostly. Some of these values may be null. We can think of this as a <code>List[Option[AnyRef]]</code>. For each value in this list, if it exists, I want to generate a string containing the value between parens; if it does not exist, then just an empty string. In addition, before the value is inserted between parens, it may need to have a transformation applied to it. For instance, if the value is an <code>Option[String]</code>, I may want to apply the <code>trim</code> function.</p>
  
  <p>I've approached this by thinking of the data structure as a map of an optional value paired with an optional function operating on that value and returning a string (I don't know how to specify that in Scala, as I think Map[Option[A], Option[(A) => String]] is not correct, as it implies that every option key is of the same type). Here's what I have written so far with example data and functions:</p>
  
  <pre><code>lazy val messageContext = Map(
    None -&gt; None,
    Some("hello") -&gt; None,
    Some(4) -&gt; Some(negate _),
    None -&gt; None
  ) map { t =&gt; wrapText(t._1, t._2)  } toList
  
  private def wrapText[A](option: Option[A], transFn: Option[(A) =&gt; String]): String = option match {
    case Some(o) =&gt; " (%s)".format(transFn.getOrElse(stringIdentity _)(o))
    case _ =&gt; ""
  }
  
  private def stringIdentity[A](a: A) = a.toString
  
  private def negate(n: Number) = n * -1
  </code></pre>
  
  <p>I'm getting a compiler error, though:</p>
  
  <pre><code>error: type mismatch;
   found   : Option[java.lang.Number =&gt; java.lang.String]
   required: Option[AnyVal{def getClass(): java.lang.Class[_ &gt;: Double with Int &lt;: AnyVal]} =&gt; String]
    ) map { t =&gt; wrapText(t._1, t._2)  } toList
  </code></pre>
  
  <p>Is my approach close on this? Is this easily possible with Scala, or would I have to resort to some scalaz magic to make this really work?</p>
  
  <p>Thanks.</p>
  
  <p>EDIT: Correct output for the above input would be this:
  <code>List[""," (hello)"," (-4)",""]</code></p>