<div dir="ltr">On Sat, Apr 23, 2016 at 4:51 AM, Michael Peternell <span dir="ltr"><<a href="mailto:michael.peternell@gmx.at" target="_blank">michael.peternell@gmx.at</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">In my opinion, requiring something is almost never something that I would describe as a "feature".</blockquote><div><br></div><div>I agree with this completely.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I don't really care if the keyword is called "optional" or "objcoptional", I think both are fine, but I wouldn't want it to read "@objc optional". The "@objc" in "@objc optional" should be optional. Also, "@objc optional" seems to imply that there is something like "optional" without "@objc" as well.</blockquote><div><br></div><div>Well, here is where I disagree. IMO, requiring this particular something (i.e. @objc) is a feature, precisely because there *is* (conceptually) optional without @objc. The point is to emphasize that it is a deliberate choice not to support this possibility in Swift-only code.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Maybe I go to a coffee house and order a coffee. After 15 minutes I ask when it will come. The waiter says that it will not come because I should have ordered a coffee "with caffeine". I complain: "but you didn't bring me any coffee at all". He said: "Yes, because we don't sell coffee without caffeine". "You should have said that", I replied angrily. So he explained it: "Our house is called 'real coffee contains caffeine', so I thought you'd know that" - "I understand." ;)<br></blockquote><div><br></div><div>I think, rather, if we're going to use your analogy, that supporting optional only in Obj-C is more like selling only coffee without caffeine. And, it's been decided that it's not appropriate to sell coffee with caffeine. Having a keyword named "optional" is like having a menu that lists "coffee, $2" while only serving decaffeinated coffee. The problem being addressed is that people read the menu outside the restaurant, enter the restaurant because they want to drink coffee, then try to order coffee only to learn from the waiter that it has no caffeine. Then people try to propose that the restaurant sell coffee with caffeine, even though that has already been proposed before and decided against. The proposal here is to rewrite the menu so that it says "notreallycoffee, $2", and what I'm suggesting is instead writing "decaffeinated coffee, $2" because it's easier to read.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
To me, it's like a restaurant owner who complains that some customers say "please" while other customers don't. For better consistency, he now forces each customer to say "please" as well and to be polite. He also tried the opposite approach, by forbidding the word "please" in his rooms. But some polite customers became very unfriendly when he told them in a harsh tone to not use the "P-Word" here. So much for the Swiftian modest proposal ;)<br>
<span class="HOEnZb"><font color="#888888"><br>
-Michael<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
> Am 23.04.2016 um 03:02 schrieb Xiaodi Wu via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>:<br>
><br>
> ...assuming, of course, that you're putting forward a draft proposal about Swift that happens to be modest in scope, and not a Swiftian modest proposal.<br>
><br>
><br>
> On Fri, Apr 22, 2016 at 7:56 PM, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com">xiaodi.wu@gmail.com</a>> wrote:<br>
> > Not an expert on Obj-C compatibility in Swift by any means, but this<br>
> > reads like it's largely a change of nomenclature. To me, though,<br>
> > `objcoptional` reads exceedingly poorly. Why not emphasize the Obj-C<br>
> > compatibility angle by requiring the `@objc` attribute to precede each<br>
> > use of `optional`? (In other words, effectively rename `optional` to<br>
> > `@objc optional`.)<br>
> ><br>
> ><br>
> > On Fri, Apr 22, 2016 at 7:35 PM, Douglas Gregor via swift-evolution<br>
> > <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
> >> Proposal link:<br>
> >> <a href="https://github.com/DougGregor/swift-evolution/blob/objc-optional/proposals/NNNN-optional-requirements.md" rel="noreferrer" target="_blank">https://github.com/DougGregor/swift-evolution/blob/objc-optional/proposals/NNNN-optional-requirements.md</a><br>
> >><br>
> >> After a whole lot of discussion and thrashing on optional requirements, I<br>
> >> have a draft for a modest proposal: change the ‘optional’ keyword to<br>
> >> something that indicates that this feature is only for compatibility with<br>
> >> Objective-C and will not be supported on Swift protocols. Comments welcome!<br>
> >><br>
> >> - Doug<br>
> >><br>
> >> Make Optional Requirements Objective-C-only<br>
> >><br>
> >> Proposal: SE-NNNN<br>
> >> Author(s): Doug Gregor<br>
> >> Status: Awaiting review<br>
> >> Review manager: TBD<br>
> >><br>
> >> Introduction<br>
> >><br>
> >> Swift currently has support for "optional" requirements in Objective-C<br>
> >> protocols, to match with the corresponding feature of Objective-C. We don't<br>
> >> want to make optional requirements a feature of Swift protocols (for reasons<br>
> >> described below), nor can we completely eliminate the notion of the language<br>
> >> (for different reasons also described below). Therefore, to prevent<br>
> >> confusion about our direction, this proposal changes the optional keyword<br>
> >> objcoptional to indicate that this is an Objective-C compatibility feature.<br>
> >><br>
> >> Swift-evolution threads: eliminate optional requirements, make Swift<br>
> >> protocols support optional requirements and make optional protocol<br>
> >> requirements first class citizens.<br>
> >><br>
> >> Motivation<br>
> >><br>
> >> Having optional only work for Objective-C requirements is very weird: it<br>
> >> feels like a general feature with a compiler bug that prevents it from<br>
> >> working generally. However, we don't want to make it a feature of Swift<br>
> >> protocols and we can't eliminate it (see alternatives considered), so we<br>
> >> propose to rename the keyword to make it clear that this feature is intended<br>
> >> only for compatibility with Objective-C.<br>
> >><br>
> >> Proposed solution<br>
> >><br>
> >> Rename the optional contextual keyword to objcoptional. Note that:<br>
> >><br>
> >> It would read better as objc_optional or objcOptional, but keywords in Swift<br>
> >> run the words together, and<br>
> >><br>
> >> It should not be an attribute @objcOptional because it changes the effective<br>
> >> type of the declaration. Referencing an optional requirement wraps the<br>
> >> result in one more level of optional, which is used to test whether the<br>
> >> requirement was implemented.<br>
> >><br>
> >> This means that:<br>
> >><br>
> >> @objc protocol NSTableViewDelegate {<br>
> >> optional func tableView(_: NSTableView, viewFor: NSTableColumn, row: Int)<br>
> >> -> NSView?<br>
> >> optional func tableView(_: NSTableView, heightOfRow: Int) -> CGFloat<br>
> >> }<br>
> >><br>
> >> becomes:<br>
> >><br>
> >> @objc protocol NSTableViewDelegate {<br>
> >> objcoptional func tableView(_: NSTableView, viewFor: NSTableColumn, row:<br>
> >> Int) -> NSView?<br>
> >> objcoptional func tableView(_: NSTableView, heightOfRow: Int) -> CGFloat<br>
> >> }<br>
> >><br>
> >> Impact on existing code<br>
> >><br>
> >> Any code that declares @objc protocols with optional requirements will need<br>
> >> to be changed to use the objcoptionalkeyword. However, it is trivial for the<br>
> >> migrator to update the code and for the compiler to provide Fix-Its, so the<br>
> >> actual impact on users should be small.<br>
> >><br>
> >> Alternatives considered<br>
> >><br>
> >> It's a fairly common request to make optional requirements work in Swift<br>
> >> protocols (as in the aforementioned threads, hereand here). However, such<br>
> >> proposals have generally met with resistance because optional requirements<br>
> >> have significant overlap with other protocol features: "default"<br>
> >> implementations via protocol extensions and protocol inheritance. For the<br>
> >> former case, the author of the protocol can provide a "default"<br>
> >> implementation via a protocol extension that encodes the default case<br>
> >> (rather than putting it at the call site). In the latter case, the protocol<br>
> >> author can separate the optional requirements into a different protocol that<br>
> >> a type can adopt to opt-in to whatever behavior they customize. While not<br>
> >> exactlythe same as optional requirements, which allow one to perform<br>
> >> per-requirement checking to determine whether the type implemented that<br>
> >> requirement, the gist of the threads is that doing so is generally<br>
> >> considered an anti-pattern: one would be better off factoring the protocol<br>
> >> in a different way. Therefore, we do not propose to make optional<br>
> >> requirements work for Swift protocols.<br>
> >><br>
> >> The second alternative would be to eliminate optional requirements entirely<br>
> >> from the language. The primary challenge here is Cocoa interoperability,<br>
> >> because Cocoa's protocols (primarily delegates and data sources) have a<br>
> >> large number of optional requirements that would have to be handled somehow<br>
> >> in Swift. These optional requirements would have to be mapped to some other<br>
> >> construct in Swift, but the code generation model must remain the same<br>
> >> because the Cocoa frameworks rely on the ability to ask the question "was<br>
> >> this requirement implemented by the type?" in Objective-C code at run time.<br>
> >><br>
> >> The most popular approach to try to map optional requirements into existing<br>
> >> Swift constructs is to turn an optional method requirement into a property<br>
> >> of optional closure type. For example, this Objective-C protocol:<br>
> >><br>
> >> @protocol NSTableViewDelegate<br>
> >> @optional<br>
> >> - (nullable NSView *)tableView:(NSTableView *)tableView<br>
> >> viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;<br>
> >> - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row;<br>
> >> @end<br>
> >><br>
> >> which currently imports into Swift as:<br>
> >><br>
> >> @objc protocol NSTableViewDelegate {<br>
> >> optional func tableView(_: NSTableView, viewFor: NSTableColumn, row: Int)<br>
> >> -> NSView?<br>
> >> optional func tableView(_: NSTableView, heightOfRow: Int) -> CGFloat<br>
> >> }<br>
> >><br>
> >> would become, e.g.,<br>
> >><br>
> >> @objc protocol NSTableViewDelegate {<br>
> >> var tableView: ((NSTableView, viewFor: NSTableColumn, row: Int) -><br>
> >> NSView?)? { get }<br>
> >> var tableView: ((NSTableView, heightOfRow: Int) -> CGFloat)? { get }<br>
> >> }<br>
> >><br>
> >> Unfortunately, this introduces an overloaded property named tableView. To<br>
> >> really make this work, we would need to introduce the ability for a property<br>
> >> to have a compound name, which would also let us take the labels out of the<br>
> >> function type:<br>
> >><br>
> >> @objc protocol NSTableViewDelegate {<br>
> >> var tableView(_:viewFor:row:): ((NSTableView, NSTableColumn, Int) -><br>
> >> NSView?)? { get }<br>
> >> var tableView(_:heightOfRow:): ((NSTableView, Int) -> CGFloat)? { get }<br>
> >> }<br>
> >><br>
> >> By itself, that is a good feature. However, we're not dont, because we would<br>
> >> need yet another extension to the language: one would want to be able to<br>
> >> provide a method in a class that is used to conform to a property in the<br>
> >> protocol, e.g.,<br>
> >><br>
> >> class MyDelegate : NSObject, NSTableViewDelegate {<br>
> >> func tableView(_: NSTableView, viewFor: NSTableColumn, row: Int) -><br>
> >> NSView? { ... }<br>
> >> func tableView(_: NSTableView, heightOfRow: Int) -> CGFloat { ... }<br>
> >> }<br>
> >><br>
> >> Indeed, the Objective-C implementation model effectively requires us to<br>
> >> satisfy these property-of-optional-closure requirements with methods so that<br>
> >> Objective-C clients can use -respondsToSelector:. In other words, one would<br>
> >> not be able to implement these requirements in by copy-pasting from the<br>
> >> protocol to the implementing class:<br>
> >><br>
> >> class MyDelegate : NSObject, NSTableViewDelegate {<br>
> >> // Note: The Objective-C entry points for these would return blocks, which<br>
> >> is incorrect<br>
> >> var tableView(_:viewFor:row:): ((NSTableView, NSTableColumn, Int) -><br>
> >> NSView?)? { return ... }<br>
> >> var tableView(_:heightOfRow:): ((NSTableView, Int) -> CGFloat)? { return<br>
> >> ... }<br>
> >> }<br>
> >><br>
> >> That is both a strange technical restriction that would be limited to<br>
> >> Objective-C protocols and a serious usability problem: the easiest way to<br>
> >> stub out the contents of your type when it conforms to a given protocol is<br>
> >> to copy the declarations from the protocol into your type, then fill in the<br>
> >> details. This change would break that usage scenario badly.<br>
> >><br>
> >> There have been other ideas to eliminate optional requirements. For example,<br>
> >> Objective-C protocols could be annotated with attributes that say what the<br>
> >> default implementation for each optional requirement is (to be used only in<br>
> >> Swift), but such a massive auditing effort is impractical. There is a<br>
> >> related notion of caller-site default implementations that was not<br>
> >> well-received due to its complexity.<br>
> >><br>
> >> _______________________________________________<br>
> >> swift-evolution mailing list<br>
> >> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
> >> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
> >><br>
><br>
> _______________________________________________<br>
> swift-evolution mailing list<br>
> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
</div></div></blockquote></div><br></div></div>