title: How to return from Scala method with generics
body: <p>I have two trait</p>
  
  <pre><code>trait TResponseData extends Serializable {} //all response data
  trait TRequestData extends Serializable {} //all request
  </code></pre>
  
  <p>Now I have <em>Response</em> class, which contains data and request</p>
  
  <pre><code>case class Response[A &lt;: TResponseData, B &lt;: TRequestData](data: A, request: B = null){}
  </code></pre>
  
  <p>While instance of case class <em>Response</em> must have data, request is not mandatory. So I declared its default to null in case class constructor. From Scala console, I am able to create its instance like</p>
  
  <pre><code>Response(null, null)
  Response(null)
  </code></pre>
  
  <p>To generate instance of <em>Response</em>, I have created helper method:</p>
  
  <pre><code>def generateResponse[A &lt;: TResponseData, B &lt;: TRequestData] (data: A, request: B, passRequestInResponse: Boolean): Response[A, B] = {
      if (passRequestInResponse) 
          return Response(data, request)
      return Response(data, null)
  }
  </code></pre>
  
  <p>Based on boolean <em>passRequestInResponse</em>, I am setting request in Response. 
  On compiling same, I am getting follow error:</p>
  
  <pre><code>error: type mismatch;
   found   : Null(null)
   required: B
             return Response(data, null)
  </code></pre>
  
  <p>How can I achieve same (making request optional in <em>generateResponse</em> method)</p>