<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 29, 2015, at 4:29 PM, Kevin Ballard <<a href="mailto:kevin@sb.org" class="">kevin@sb.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">On Tue, Dec 29, 2015, at 01:24 PM, Matthew Johnson wrote:<br class=""></div><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="">Hi Kevin,<br class=""></div><div class=""> </div><div class="">Thanks for taking time to look at the proposal.<br class=""></div><div class=""> </div><div class="">The technique you show here is not bad, but it has several deficiencies IMO which are addressed by the solution in the proposal.<br class=""></div><div class=""> </div><div class=""><div class="">1. Forwarding should be an implementation detail, not exposed as it is with this method.<br class=""></div><div class="">2. As such, the getter for the forwardee is visible. The forwardee is an implementation detail that should not be visible in most cases.<br class=""></div></div></blockquote><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">I was tempted to call it `_forwardedSequence` instead, but I chose not to do that because the _prefix convention right now means "stdlib things that we don't want to expose but have to do so because of implementation details”.<br class=""></div></div></blockquote><div><br class=""></div><div>Sure, but naming conventions are not access control.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> </div><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class=""><div class="">3. There is no way to specify access control for the synthesized methods and if there were it would be coupled to the access control of the conformance.<br class=""></div></div></blockquote><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">I'm not sure what you mean by this. Access control for these methods should work identically to access control for the original protocol. The forwarder protocol would simply be declared with the same access control.<br class=""></div></div></blockquote><div><br class=""></div><div>There are 3 things at play here:</div><div><br class=""></div><div>1. The protocol that is forwarded</div><div>2. The forwarding methods implementing the protocol requirements</div><div>3. Conformance to the protocol</div><div><br class=""></div><div>When you write code manually it is possible to specify distinct access control for 1 and 2, and as Joe noted it would be nice to be able to specify access control for 3 as well. If this becomes possible all 3 could have distinct access levels. </div><div><br class=""></div><div>For example, a public protocol, internal implementing methods, but private conformance. Or public implementing methods, an internal protocol, and private conformance. Or a public protocol, and internal implementing methods and conformance. Or public implementing methods, but an internal protocol and internal conformance. </div><div><br class=""></div><div>Some of these combinations might be unusual, but there may be use cases for them. Ideally a forwarding mechanism would allow granular control, just as is possible with manual forwarding implementations.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> </div><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class=""><div class="">4. You can't forward to a type that doesn't conform to the protocol. This is particularly important for existential forwardees (at least until they conform to their protocol).<br class=""></div></div></blockquote><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">It seems weird to me that your forwarding proposal allows you to forward to a member that doesn't conform to the protocol. I suppose it makes some sense if you only forward some methods and implement the others yourself, but I'm not convinced there's actually a good reason to support this. What circumstances are you thinking of where you'd actually find yourself forwarding to a member that doesn't conform to the protocol (that isn't an existential)? The only case that really comes to mind is when the member is of a generic type that can't conform to the protocol (e.g. Array<Int> doesn't conform to Equatable), but the solution there is to allow for conditional protocol conformance (which we already know is something we want in the language).<br class=""></div></div></blockquote><div><br class=""></div><div>The proposal requires the forwardee to be *conform<b class="">able</b>* but not necessarily conform<b class="">ing</b>. The difference is admittedly subtle, but there may be good reasons. It may be that it isn’t desriable for the forwardee to actually conform for one reason or another, maybe because you don’t want it to be possible to cast values of the forwardee type to the existential type of the protocol. </div><div><br class=""></div><div>The reason the proposal doesn’t <b class="">require</b> actual conformance is simply because it isn’t necessary to synthesize the forwarding members and there are potential use cases that could take advantage of the additional flexibility. And again, because actual conformance would not be required to write the forwarding methods manually.</div><div><br class=""></div><div>Protocols are an essential part of the forwarding mechanism because they are the only place in the language to differentiate between Self and a parameter or return value that should be always be of a specific type. However, actual conformance to the protocol by either the forwarder or the forwardee is not necessary to implement forwarding. Rather than arbitrarily require that it seems best to allow users control over whether, how, and where actual conformance is declared by both the forwarder and the forwardee.</div><div><br class=""></div><div>The proposal specifically does not support “partial forwarding”. That was noted as a possible future enhancement. If such an enhancement were ever brought forward I would not support dropping the requirement for the forwardee to be “theoretically conformable” to the protocol. Partial forwarding would only exist to enable forwarders to “override” specific member implementations.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Forwarding to existentials is a valid concern, but I'm not actually sure why Swift doesn't make existential protocol values conform to the protocol anyway. That limitation would make sense if you could create existential protocol values from protocols with Self/associated types where the existential simply omits the relevant members (because then it obviously doesn't conform to the protocol), but Swift doesn't actually let you create existentials in that situation anyway.<br class=""></div></div></blockquote><div><br class=""></div><div>Yes, I agree that existential should conform to the corresponding protocol and I hope it is a temporary limitation. It has been mentioned that there are some implementation complexities and possible performance issues to work through. </div><div><br class=""></div><div>In any case, until this happens existential are a strong use case for taking advantage of forwarding to "theoretically conformable but not actually conforming” types. There is no reason not to support this use case and no reason not to enable any other use cases that might take advantage of that flexibility.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> </div><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class=""><div class="">5. A type that can't conform to the protocol can't forward an implementation of the members of the protocol. Specifically, non-final classes cannot automatically forward a group of methods to an implementer.<br class=""></div></div></blockquote><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">It sounds like you're talking here about the ability to forward members that aren't actually associated with a protocol, right? I don't see why you can't just declare a protocol in that case to represent the members that you want to be able to forward.</div></div></blockquote><div><br class=""></div><div>No, I’m talking about the case where you might have a protocol with a Self return type. non-final classes cannot conform to such a protocol. But it is possible to synthesize forwarding methods corresponding to a protocol where the return type of those methods is Forwarder. You can’t declare conformance and don’t actually conform, but the forwarding methods could be synthesized without trouble and would work just fine in cases where a return type of Forwarder (rather than a covariant return type) is acceptable. </div><br class=""><blockquote type="cite" class=""><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> </div><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class=""><div class="">6. This solution does not lay the foundation for a newtype feature. It would be possible to have a specialized newtype feature, but why not build it on top of a more general forwarding feature?<br class=""></div></div></blockquote><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Barring a detailed newtype proposal, it's hard to see how forwarding would actually interact with it. Are you thinking that a newtype would have some member that provides the underlying value (e.g. a `var rawValue`)? Given such a member, a generalized forwarding mechanism would then interact with it. But if e.g. newtype works by letting you cast (with `as`) to the underlying type, then your generalized forwarding mechanism can't actually work unless it has two different modes (one of which uses a member and the other uses a cast), which means you're specializing for newtype anyway.<br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Besides, when using a newtype mechanism like that, since the newtype has the same in-memory representation as the original type, you don't actually want to generate new methods at all for forwarding, instead you just want to re-use the exact same methods as the original type (otherwise you're just bloating your code with a bunch of stubs that do nothing more than bitcast the value and call the original method).</div></div></blockquote><div><br class=""></div><div>I roughly sketched what a newtype mechanism built on top of protocol forwarding might look like in a simple case in the future enhancements section of the proposal. This is not a detailed proposal by any means, but it does suggest a possible direction. The suggested direction is pretty similar to what Joe Groff mentioned in this post: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001137.html" class="">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001137.html</a>.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">-Kevin Ballard</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> </div><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="">You may be right that many protocols are not amenable to forwarding. The motivation for this proposal is that it enables delegation-based designs to be implemented succinctly. In that use case the protocols will be designed alongside concrete implementations and types that forward to them. A secondary motivation for this proposal is to lay a foundation for a newtype feature. In that case the protocols to be forwarded would be specifically designed to represent the portion of the interface of the wrapped type which should be visible to users of the newtype.<br class=""></div><div class=""> </div><div class="">I hope these points might be enough to convince you that it is worth a closer look.<br class=""></div><div class=""> </div><div class="">Matthew<br class=""></div><div class=""><div class=""> </div><div class=""><blockquote type="cite" class=""><div class="">On Dec 29, 2015, at 2:06 PM, Kevin Ballard via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></div><div class=""> </div><div class=""><div class=""><div class="">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 class=""></div><div class=""> </div><div class="">For example, if I want to automatically forward SequenceType to a member, I can do something like<br class=""></div><div class=""> </div><div class=""><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">protocol</span><span class="Apple-converted-space"> </span>SequenceTypeForwarder :<span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(112, 61, 170);">SequenceType</span><span class="Apple-converted-space"> </span>{<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">typealias</span><span class="Apple-converted-space"> </span>ForwardedSequenceType : SequenceType<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space"> </span>forwardedSequence :<span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(112, 61, 170);">ForwardedSequenceType</span><span class="Apple-converted-space"> </span>{<span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">get</span><span class="Apple-converted-space"> </span>}<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span class="colour" style="color: rgb(187, 44, 162);">extension</span><span class=""></span>SequenceTypeForwarder<span class=""><span class="Apple-converted-space"> </span>{</span><br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span class=""></span><span class="colour" style="color: rgb(187, 44, 162);">func</span><span class=""><span class="Apple-converted-space"> </span>generate() -><span class="Apple-converted-space"> </span></span>ForwardedSequenceType<span class="">.</span>Generator<span class=""><span class="Apple-converted-space"> </span>{</span><br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span>forwardedSequence.generate()<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space"> </span>underestimateCount() -><span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(112, 61, 170);">Int</span><span class="Apple-converted-space"> </span>{<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span>forwardedSequence.underestimateCount()<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space"> </span>map<T>(<span class="colour" style="color: rgb(187, 44, 162);">@noescape</span><span class="Apple-converted-space"> </span>transform: (ForwardedSequenceType.Generator.Element)<span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">throws</span><span class="Apple-converted-space"> </span>-> T)<span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">rethrows</span><span class="Apple-converted-space"> </span>-> [<span class="colour" style="color: rgb(112, 61, 170);">T</span>] {<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">try</span><span class="Apple-converted-space"> </span>forwardedSequence.map(transform)<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space"> </span>filter(<span class="colour" style="color: rgb(187, 44, 162);">@noescape</span><span class="Apple-converted-space"> </span>includeElement: (ForwardedSequenceType.Generator.Element)<span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">throws</span><span class="Apple-converted-space"> </span>-> Bool)<span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">rethrows</span><span class="Apple-converted-space"> </span>-> [<span class="colour" style="color: rgb(112, 61, 170);">ForwardedSequenceType</span>.<span class="colour" style="color: rgb(112, 61, 170);">Generator</span>.<span class="colour" style="color: rgb(112, 61, 170);">Element</span>] {<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">try</span><span class="Apple-converted-space"> </span>forwardedSequence.filter(includeElement)<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space"> </span>forEach(<span class="colour" style="color: rgb(187, 44, 162);">@noescape</span><span class="Apple-converted-space"> </span>body: (ForwardedSequenceType.Generator.Element)<span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">throws</span><span class="Apple-converted-space"> </span>-> Void)<span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">rethrows</span><span class="Apple-converted-space"> </span>{<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">try</span><span class="Apple-converted-space"> </span>forwardedSequence.forEach(body)<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span class=""></span><span class="colour" style="color: rgb(187, 44, 162);">func</span><span class=""><span class="Apple-converted-space"> </span>dropFirst(n:<span class="Apple-converted-space"> </span></span>Int<span class="">) -><span class="Apple-converted-space"> </span></span>ForwardedSequenceType<span class="">.</span>SubSequence<span class=""><span class="Apple-converted-space"> </span>{</span><br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span>forwardedSequence.dropFirst(n)<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span class=""></span><span class="colour" style="color: rgb(187, 44, 162);">func</span><span class=""><span class="Apple-converted-space"> </span>dropLast(n:<span class="Apple-converted-space"> </span></span>Int<span class="">) -><span class="Apple-converted-space"> </span></span>ForwardedSequenceType<span class="">.</span>SubSequence<span class=""><span class="Apple-converted-space"> </span>{</span><br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span>forwardedSequence.dropLast(n)<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span class=""></span><span class="colour" style="color: rgb(187, 44, 162);">func</span><span class=""><span class="Apple-converted-space"> </span>prefix(maxLength:<span class="Apple-converted-space"> </span></span>Int<span class="">) -><span class="Apple-converted-space"> </span></span>ForwardedSequenceType<span class="">.</span>SubSequence<span class=""><span class="Apple-converted-space"> </span>{</span><br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span>forwardedSequence.prefix(maxLength)<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span class=""></span><span class="colour" style="color: rgb(187, 44, 162);">func</span><span class=""><span class="Apple-converted-space"> </span>suffix(maxLength:<span class="Apple-converted-space"> </span></span>Int<span class="">) -><span class="Apple-converted-space"> </span></span>ForwardedSequenceType<span class="">.</span>SubSequence<span class=""><span class="Apple-converted-space"> </span>{</span><br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span>forwardedSequence.suffix(maxLength)<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space"> </span>split(maxSplit: Int, allowEmptySlices: Bool,<span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">@noescape</span><span class="Apple-converted-space"> </span>isSeparator: (ForwardedSequenceType.Generator.Element)<span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">throws</span><span class="Apple-converted-space"> </span>-> Bool)<span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">rethrows</span><span class="Apple-converted-space"> </span>-> [<span class="colour" style="color: rgb(112, 61, 170);">ForwardedSequenceType</span>.<span class="colour" style="color: rgb(112, 61, 170);">SubSequence</span>] {<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">try</span><span class="Apple-converted-space"> </span>forwardedSequence.split(maxSplit, allowEmptySlices: allowEmptySlices, isSeparator: isSeparator)<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}<br class=""></div></div><div class=""> </div><div class="">With this protocol declared, I can then say something like<br class=""></div><div class=""> </div><div class=""><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">struct</span><span class="Apple-converted-space"> </span>Foo {<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space"> </span>ary: [<span class="colour" style="color: rgb(112, 61, 170);">Int</span>]<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span class="colour" style="color: rgb(187, 44, 162);">extension</span><span class=""></span>Foo<span class=""><span class="Apple-converted-space"> </span>:<span class="Apple-converted-space"> </span></span>SequenceTypeForwarder<span class=""><span class="Apple-converted-space"> </span>{</span><br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="colour" style="color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space"> </span>forwardedSequence: [<span class="colour" style="color: rgb(112, 61, 170);">Int</span>] {<span class="Apple-converted-space"> </span><span class="colour" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span>ary }<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}<br class=""></div></div><div class=""> </div><div class="">and my struct Foo now automatically implements SequenceType by forwarding to its variable `ary`.<br class=""></div><div class=""> </div><div class="">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 class=""></div><div class=""> </div><div class="">-Kevin Ballard<br class=""></div><div class=""><img border="0" height="1" width="1" alt="" src="https://www.fastmailusercontent.com/proxy/19d6d3ba9d65bf59fd6cecf5353444fa4729ec326d468f378628d572dee23bba/8647470737a3f2f25723030323431303e23647e23756e64676279646e2e65647f27766f2f60756e6f35707e6d356c464d427b4444583962487a5d2236426e6b6d22324a51767353686e4e4d2236467958546365614034573658777b6146613a795171403564437368426849335449775a6a7747564c49686a5e637f6a797974377d475345393c453b6861435950703f695931364544654d22364d6544536a4966423f4a77393630307149415276354f467d62474b4d46636d223649353d696d4a65603c416e62435c6835785c4976466d4d6036645431645d2232473478475a44403d46476f415d2236456d60536f64653b633d2236457e476a5a556450746832314e4074537851705277473a7d444332373158335341666f627c6d486a7244557644587d22324941494d23344/open" style="height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><br class=""></div></div><div class="">_______________________________________________<br class=""></div><div class="">swift-evolution mailing list<br class=""></div><div class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""></div><div class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></div></blockquote></div></div></blockquote></div></blockquote></div><br class=""></body></html>