title: How to create a function that returns a generic protocol type?
body: <p>In the following code, I have a generic protocol <code>Var</code> and it have a private implementation class <code>LambdaVar</code>. Now I want to have a function that return an instance of the <code>LambdaVar</code> without expose it publicly. </p>
  
  <p>But I can't find a way to define a function that return a generic protocol type.</p>
  
  <pre><code>public protocol Var {
      typealias ValueType
      var value : ValueType { get }
  }
  
  struct LambdaVar&lt;T&gt; : Var {
      let _get : Void -&gt; T
      init(_ f : Void -&gt; T)
      {
          _get = f
      }
      var value : T {
          get {
              return _get()
          }
      }
  }
  
  public func transform&lt;T : Var, U&gt;(v : T, f : T.ValueType -&gt; U) -&gt; Var&lt;U&gt; {
      return LambdaVar&lt;U&gt;() { f(v.value) }                       // ^^^^^^ what to put here?
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Playground execution failed: &lt;EXPR&gt;:26:67: error: cannot specialize non-generic type 'Var'
  public func transform&lt;T : Var, U&gt;(v : T, f : T.ValueType -&gt; U) -&gt; Var&lt;U&gt; {
                                                                    ^
  &lt;EXPR&gt;:26:67: error: protocol 'Var' can only be used as a generic constraint because it has Self or associated type requirements
  public func transform&lt;T : Var, U&gt;(v : T, f : T.ValueType -&gt; U) -&gt; Var&lt;U&gt; {
  </code></pre>