<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>I briefly skimmed your proposal, so I apologize if you already addressed this, but it occurs to me that we could support automatic protocol forwarding today on a per-protocol basis simply by declaring a separate protocol that provides default implementations doing the forwarding. Handling of Self return types can then be done by adding a required initializer (or just not implementing that method, so the concrete type is forced to deal with it even though everything else is forwarded).<br></div>
<div> </div>
<div>For example, if I want to automatically forward SequenceType to a member, I can do something like<br></div>
<div> </div>
<div><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">protocol</span> SequenceTypeForwarder : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">SequenceType</span> {</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">typealias</span> ForwardedSequenceType : SequenceType</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">var</span> forwardedSequence : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">ForwardedSequenceType</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">get</span> }</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>SequenceTypeForwarder<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> {</span></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">func</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> generate() -> </span>ForwardedSequenceType<span style="font-variant-ligatures: no-common-ligatures; color: #000000">.</span>Generator<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> {</span></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> forwardedSequence.generate()</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">func</span> underestimateCount() -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Int</span> {</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> forwardedSequence.underestimateCount()</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">func</span> map<T>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">@noescape</span> transform: (ForwardedSequenceType.Generator.Element) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">throws</span> -> T) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">rethrows</span> -> [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">T</span>] {</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">try</span> forwardedSequence.map(transform)</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">func</span> filter(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">@noescape</span> includeElement: (ForwardedSequenceType.Generator.Element) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">throws</span> -> Bool) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">rethrows</span> -> [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">ForwardedSequenceType</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Generator</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Element</span>] {</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">try</span> forwardedSequence.filter(includeElement)</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">func</span> forEach(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">@noescape</span> body: (ForwardedSequenceType.Generator.Element) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">throws</span> -> Void) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">rethrows</span> {</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">try</span> forwardedSequence.forEach(body)</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">func</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> dropFirst(n: </span>Int<span style="font-variant-ligatures: no-common-ligatures; color: #000000">) -> </span>ForwardedSequenceType<span style="font-variant-ligatures: no-common-ligatures; color: #000000">.</span>SubSequence<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> {</span></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> forwardedSequence.dropFirst(n)</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">func</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> dropLast(n: </span>Int<span style="font-variant-ligatures: no-common-ligatures; color: #000000">) -> </span>ForwardedSequenceType<span style="font-variant-ligatures: no-common-ligatures; color: #000000">.</span>SubSequence<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> {</span></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> forwardedSequence.dropLast(n)</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">func</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> prefix(maxLength: </span>Int<span style="font-variant-ligatures: no-common-ligatures; color: #000000">) -> </span>ForwardedSequenceType<span style="font-variant-ligatures: no-common-ligatures; color: #000000">.</span>SubSequence<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> {</span></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> forwardedSequence.prefix(maxLength)</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">func</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> suffix(maxLength: </span>Int<span style="font-variant-ligatures: no-common-ligatures; color: #000000">) -> </span>ForwardedSequenceType<span style="font-variant-ligatures: no-common-ligatures; color: #000000">.</span>SubSequence<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> {</span></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> forwardedSequence.suffix(maxLength)</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">func</span> split(maxSplit: Int, allowEmptySlices: Bool, <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">@noescape</span> isSeparator: (ForwardedSequenceType.Generator.Element) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">throws</span> -> Bool) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">rethrows</span> -> [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">ForwardedSequenceType</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">SubSequence</span>] {</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">try</span> forwardedSequence.split(maxSplit, allowEmptySlices: allowEmptySlices, isSeparator: isSeparator)</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</p></div>
<div> </div>
<div>With this protocol declared, I can then say something like<br></div>
<div> </div>
<div><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">struct</span> Foo {</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">var</span> ary: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Int</span>]</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>Foo<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> : </span>SequenceTypeForwarder<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> {</span></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">var</span> forwardedSequence: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Int</span>] { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> ary }</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</p></div>
<div> </div>
<div>and my struct Foo now automatically implements SequenceType by forwarding to its variable `ary`.<br></div>
<div> </div>
<div>The downside to this is it needs to be manually declared for each protocol. But I wager that most protocols actually aren't really amenable to forwarding anyway.<br></div>
<div> </div>
<div>-Kevin Ballard</div>
</body>
</html>