<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><span></span></div><div><div>Ahh, I misunderstood a piece of your initial proposal. I don't have any strong opposition to a named object declaration, and there have been several occasions where I might have used such a construct (whether or not you're a fan of singletons they are very much a part of Cocoa and associated software).</div><div><br></div><div id="AppleMailSignature">That being said there are concerns (unit testing especially as was already mentioned) that may cause problems.&nbsp;</div><div id="AppleMailSignature"><br></div><div>On Dec 13, 2015, at 18:28, Marc Knaup &lt;<a href="mailto:marc@knaup.koeln">marc@knaup.koeln</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Well anonymous objects are one way object declarations could be used. They may or may not be reduce clarity - I have no strong preference here.<div><br><div>But they can also be named. This is what interests me most.</div></div><div><br></div><div>Additionally when the compiler knows that there is always exactly one instance it can probably optimize the code even further.</div><div>But I cannot answer that question.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Dec 14, 2015 at 1:17 AM, Cole Kurkowski <span dir="ltr">&lt;<a href="mailto:crk@fastmail.com" target="_blank">crk@fastmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>I think anonymous classes run counter to Swift's goals of clarity. You don't even have to pollute your whole module with the class, you can declare a private class in the same file and use it where you need it. This is, in my opinion far cleaner. Additionally, anonymous classes are really difficult for people to understand if they've not encountered the concept before.</div><div><div class="h5"><div><br>On Dec 11, 2015, at 19:48, Marc Knaup via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><br></div></div></div><blockquote type="cite"><div><div><div class="h5"><div dir="ltr"><div>@Andrey</div><div><ul><li>Note that it should also be possible to create structs anonymously as they also can implement protocols.<br></li><li>Delegates are a bit tricky because they are usually retained weakly so the anonymous object would be deallocated immediately.<br></li><li>Also using "<font face="monospace, monospace">class: XYZ {}</font>" doesn't allow creating named objects like "<font face="monospace, monospace">class&nbsp;object EmptyObject {}</font>".</li></ul></div><div><div>When talking about singletons people tend to first think at a very large scale and about abstraction, factories, testability and whatever.</div><div>It's the little things which capture my interest more, like <font face="monospace, monospace">NSNull</font> for example.</div><div><br></div><div><font face="monospace, monospace">class object NSNull {}</font></div><div><br></div><div>Or maybe a JSON Null:</div><div><br></div><div><font face="monospace, monospace">protocol JSONNode {}</font></div><div><font face="monospace, monospace">struct JSON {</font></div><div><font face="monospace, monospace">&nbsp; &nbsp; struct object Null: </font><span style="font-family:monospace,monospace">JSONNode</span><font face="monospace, monospace">&nbsp;{}</font></div><div><font face="monospace, monospace">&nbsp; &nbsp; struct Bool { … } // not an object but a type</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">&nbsp; &nbsp; // …</font></div><div><font face="monospace, monospace">}<br></font><div><div><br></div><div>Now the interesting part about a singleton <font face="monospace, monospace">NSNull</font>&nbsp;(or the JSON one) is that it represents both, a type and an instance.</div><div><br></div><div><font face="monospace, monospace">array.append(NSNull)</font></div><div><br></div><div>Note that we cannot use a <font face="monospace, monospace">class</font> and call&nbsp;<font face="monospace, monospace">NSNull()</font>&nbsp;as this would create a new instance instead of returning the singleton instance.<br></div><div>We would have to make a silly workaround like <font face="monospace, monospace">NSNull.null</font><font face="arial, helvetica, sans-serif"> or </font><font face="monospace, monospace">NSNull.sharedInstance.</font></div><div><br></div><div>Examples for <font face="monospace, monospace">JSON.Null</font>:</div><div><br></div><div><font face="monospace, monospace">jsonArray.append(JSON.Null) // used as instance</font></div><div><div><font face="monospace, monospace"><br>if jsonNode is JSON.Null { // used as type</font></div><div><font face="monospace, monospace">&nbsp; &nbsp;// …</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">let expectedJsonTypes: [JSONNode.Type] = [JSON.Null, JSON.Bool, …]</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">if jsonNode is JSON.Null { // used as type</font></div><div><font face="monospace, monospace">&nbsp; &nbsp;// …</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace">if jsonNode == JSON.Null { // used as instance (assuming Equatable conformance)</font></div><div><font face="monospace, monospace">&nbsp; &nbsp;// …</font></div><div><font face="monospace, monospace">}</font></div></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Dec 12, 2015 at 2:29 AM, Andrey Tarantsov via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">I think that anonymous classes got lost amongst all the singletonphobia here, and yet making an anonymous local delegate class is often helpful.<br>
<br>
Perhaps we don't need an additional keyword, though; something like this could work too:<br>
<br>
view.delegate = class: SomeDelegate {<br>
&nbsp; ...<br>
}()<br>
<br>
or perhaps you want a slightly customized object:<br>
<br>
view = class: UITextField {<br>
&nbsp; func canBecomeFirstResponder() -&gt; Bool { return false }<br>
}()<br>
<br>
Of course, we could just use a named local class, like others have pointed out, but unless you really want to name that thing (and the name would often be stupid), that's a just workaround for a lack of anonymous classes.<br>
<span><font color="#888888"><br>
A.<br>
</font></span><div><div><br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">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>
</div></div></blockquote></div><br></div></div></div></div></div></div>
</div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=-2Bxb47WeGoqMoAgzLM1ObomDZ33eAoWUyfgnPK0aY0kmw8uTnsVncs2BjfvkYmaSvei-2BQMMuRREhmfU5-2FAi-2BxqPr-2B9ojSqhVcRM8bQZ4uVLoNwKL3ECoytxX96nLBBfDCAlguZ2m76ZF2iwOKAdVPJn-2Fh9jaa6YqziTB7Yml5eMONFll4zmhFHOZBf5ylWhbPCf2QAa047NFmAHN-2F-2FaSJ4aBdQb8b6drWzISmhPx94Xs-3D" alt="" width="1" height="1" border="0" style="min-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">
</div></blockquote><span class=""><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></span></div></blockquote></div><br></div>
</div></blockquote></div></div></body></html>