title: Scala function/method parametrized return type
body: <p>I have just come up with something that I don't understand. I'm trying to return a parametrized type from a method, the code is the following (working example):</p>
  
  <pre><code>trait Expression
  
  case class ExpImp1() extends Expression
  case class ExpImp2() extends Expression
  
  object Main {
  
    private def testParametrizedTypes[T](): T = {
      ExpImp1()
    }
  
    private def testWrappedParametrizedTypes[T &lt;: Expression](): Option[T] = {
      Some(new ExpImp1())
    }
  
    def main(args: Array[String]) {
  
    }
  
  }
  </code></pre>
  
  <p>but when I compile this code, the compiler says (the IDE warns me about the same error in both functions):</p>
  
  <pre><code>Error:(10, 12) type mismatch;
   found   : ExpImp1
   required: T
      ExpImp1()
             ^
  </code></pre>
  
  <p>I think that the code showed previously should work. In the first method I'm returning a type T and T has no constraint. The compiler error also occurs in the second method, but the parametrized type conforms to the returned object type too.</p>
  
  <p>Also, I wrote a code a few weeks ago that looked like that that compiled with no problems (this is not a working example as this code is part of a Play Framework project):</p>
  
  <pre><code>class CanBeAuthenticatedRequest[A](val request: Request[A]) extends WrappedRequest[A](request)
  
  class UnauthenticatedRequest[A](override val request: Request[A]) extends CanBeAuthenticatedRequest(request)
  
  class AuthenticatedRequest[A](val user: String, override val request: Request[A]) extends CanBeAuthenticatedRequest[A](request)
  
  object CanBeAuthenticatedAction extends ActionBuilder[CanBeAuthenticatedRequest] {
  
    def invokeBlock[A]...
    }
  
    object Fold {
  
      private def partialFunctionBuilder[T](authenticated: (AuthenticatedRequest[_]) =&gt; T)
                                           (unauthenticated: (UnauthenticatedRequest[_]) =&gt; T):
      PartialFunction[CanBeAuthenticatedRequest[_], T] = {
        case ar: AuthenticatedRequest[_] =&gt; authenticated(ar)
        case ur: UnauthenticatedRequest[_] =&gt; unauthenticated(ur)
      }
  
      def apply[T](request: CanBeAuthenticatedRequest[_])
                 (authenticated: (AuthenticatedRequest[_]) =&gt; T)
                 (unauthenticated: (UnauthenticatedRequest[_]) =&gt; T): T = {
        partialFunctionBuilder(authenticated)(unauthenticated)(request)
      }
  
    }
  
  }
  </code></pre>
  
  <p>As you can see, I successfully defined the function "partialFunctionBuilder" and I'm using it.</p>
  
  <p>"partialFunctionBuilder" follows the same pattern, returning a type that is parametrized with "T". I don't see what's the difference between the code I showed first and the Play Framework code. Removing "&lt;: Expression" from the "testWrappedParametrizedTypes" keeps giving me the same error.</p>