title: Scala Generics : Cannot write an instance of T to HTTP response. Try to define a Writeable[T]
body: <p>Following code when written using generic give a compilation error. </p>
  
  <p><strong>Without Generic</strong></p>
  
  <pre><code>def getData(id: String) = Action.async {
  
      val items = getItems(id)
  
      sendResult(items)
    }
  
    private def sendResult(result: Future[Any]) = {
  
  
      result.map {
        items =&gt; {
          try {
            val itemStr = items.asInstanceOf[String]
            Ok(itemStr)
          } catch {
            case t: ClassCastException =&gt; InternalServerError(s"Casting Exception while processing output $t")
          }
        }
      }.recover {
        case t:TimeoutException =&gt; InternalServerError("Api Timed out")
        case t: Throwable =&gt; InternalServerError(s"Exception in the api $t")
      }
    }
  </code></pre>
  
  <p><strong>With Generic</strong></p>
  
  <pre><code>  def getData(id: String) = Action.async {
  
      val items = getItems(id)
  
      sendResult[String](items)
    }
  
    private def sendResult[T](result: Future[Any]) = {
  
  
      result.map {
        items =&gt; {
          try {
            val itemStr = items.asInstanceOf[T]
            Ok(itemStr)
          } catch {
            case t: ClassCastException =&gt; InternalServerError(s"Casting Exception while processing output $t")
          }
        }
      }.recover {
        case t:TimeoutException =&gt; InternalServerError("Api Timed out")
        case t: Throwable =&gt; InternalServerError(s"Exception in the api $t")
      }
    }
  </code></pre>
  
  <p>The code is part of play app's contorller method. First one works fine. Second one gives following compilation error</p>
  
  <blockquote>
    <p>Cannot write an instance of T to HTTP response. Try to define a
    Writeable[T] [error]           Ok(itemStr) [error]</p>
  </blockquote>