<div id="compose" contenteditable="true" style="padding-left: 20px; padding-right: 20px; padding-bottom: 8px;">I don't think this is necessary, but if it does go through, I would prefer the `?` be optional, as occasionally I make my delegates `unowned` so that I don't have to do a bunch of nil handling/coalescing in the case where the delegate strictly owns the other object and both will be deallocated together.</div><div id="compose" contenteditable="true" style="padding-left: 20px; padding-right: 20px; padding-bottom: 8px;">The stack and multiple declarations are interesting, but if Swift 3 brings us the generics we need, those should be possible to implement without adding new keywords/complexity to the language.</div><div id="compose" contenteditable="true" style="padding-left: 20px; padding-right: 20px; padding-bottom: 8px;"><div class="acompli_signature">Alex Popov Jr.<br>Principal iOS developer | Shelfie<br></div><br></div>
    <div class="gmail_quote">_____________________________<br>From: Ross O'Brien via swift-evolution &lt;<a dir="ltr" href="mailto:swift-evolution@swift.org" x-apple-data-detectors="true" x-apple-data-detectors-type="link" x-apple-data-detectors-result="1">swift-evolution@swift.org</a>&gt;<br>Sent: Sunday, January 3, 2016 17:07<br>Subject: [swift-evolution] Idea: delegates as protocols and property types with specialised behaviours.<br>To: swift-evolution &lt;<a dir="ltr" href="mailto:swift-evolution@swift.org" x-apple-data-detectors="true" x-apple-data-detectors-type="link" x-apple-data-detectors-result="3">swift-evolution@swift.org</a>&gt;<br><br><br>    <div dir="ltr">   <div>    At the moment, the delegate pattern is just one use of protocols. We create a protocol with a set of declared functions, implemented either in a protocol extension or in the conforming type - the delegate - and another type calls functions of its delegate, notifying them when certain events occur.    <br>   </div>   <div>    <br>   </div>   <div>    I'm wondering if uses of the delegate pattern can or should be made more explicitly readable. The form I have in mind creates a keyword 'delegate', which is used in two contexts; one to replace 'protocol' in the declaration of a protocol intended as a delegate, and one to declare a property of the delegate type.   </div>   <div>    <br>   </div>   <div>    So, for example:   </div>   <div>    delegate UITableViewDataSource { }   </div>   <div>    <br>   </div>   <div>    and   </div>   <div>    class UITableView   </div>   <div>    {   </div>   <div>    &nbsp; &nbsp; delegate dataSource : UITableViewDataSource   </div>   <div>    &nbsp; &nbsp; delegate delegate : UITableViewDelegate   </div>   <div>    }   </div>   <div>    <br>   </div>   <div>    (I realise this example uses 'delegate' both as a keyword and a property name, and would cause a lot of rewriting for types which generically refer to their delegates with the property name 'delegate', so perhaps this isn't the best choice of keyword, but bear with me, the idea may still have merit.)   </div>   <div>    <br>   </div>   <div>    As a term for declaring a property, 'delegate' could be synonymous with 'weak var', and the type would be implicitly a non-force-unwrapped optional. I can't presently think of a type in Objective C or Swift which requires a strong reference to its delegate. (It might be preferred that the "?" remain explicit for readability).   </div>   <div>    <br>   </div>   <div>    Here are two suggestions from where delegates could benefit from being distinct types.   </div>   <div>    <br>   </div>   <div>    First: if the delegate pattern is observational (i.e. the type with a delegate property calls its delegate's functions as notifications), then there can be a need for multiple observers (e.g. multiple services in an app being notified that location services have been started).   </div>   <div>    (I've tried implementing something like this; an array of references to observer-type delegates at the moment maintains strong references to all delegates, so I've created a custom struct with a weak reference to the specific protocol's type and called 'forEach' on a sequence of such structs; if the reference is nil, the struct can be removed from the sequence. Much as I'd like this struct to be generic, I don't think I can create a generic struct with a protocol as the associated type.)   </div>   <div>    This might be declared with a keyword in the same way properties are modified with lazy / weak:   </div>   <div>    multiple delegate : CLLocationManagerDelegate?   </div>   <div>    <br>   </div>   <div>    Second: the delegate pattern, if applied to a group of delegates, might not be strictly observational, but might also respond in some way - a sequence of delegates could be called in turn, with candidates being asked if they can respond, returning false if they can't, true if they can.   </div>   <div>    <br>   </div>   <div>    Third: delegates could also form a stack. For example, in an NSXMLParser, the parser's delegate can change during parsing, as types handle their 'layer' of parsing and then pass control to child or parent parser-delegate types. This could be simplified to calling something akin to: 'parser.delegate.push(childType)' or 'parser.delegate.pop()'   </div>   <div>    This might be declared like this:   </div>   <div>    stack delegate : NSXMLParserDelegate?   </div>   <div>    <br>   </div>   <div>    I may be combining too many ideas into one proposal, or there may be more ideas others can add. Is this worthy of discussion?   </div>   <div>    <br>   </div>   <div>    -- Ross O'Brien   </div>  </div>   <img src="https://u2002410.ct.sendgrid.net/wf/open?upn=CmwAv3oRa0AH4Hd1bWC6X-2BzbhPqo1YEo6mPHEujr90sxEMzU-2B7awSFwp0N2BkVtbYBp-2F-2Ff11y7LFm6Ret9SMalYWFe4-2Fu-2B34nzH7fKeJkeHPpx1RQKClGlK7C-2FHH4Bi7VB-2FI4zYcoxUj63v37XgxCy2J8O6iKdBax1t1Bw-2BI3H6pnH-2BG45xjCySE8QEznFKJQvA3kUuf-2F2BRNvnDWd9Y4US7VddhsqrMDrvjX93BX1s-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;">  <br><br></div>