<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>I like it. Just a minor suggestion: as an object is a value and not a type it should have a name starting with lower case, i.e.</div><div><br></div><div>object globalResource { ... }</div><div><br></div><div>-Thorsten </div><div><br>Am 11.12.2015 um 23:07 schrieb Marc Knaup via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>:<br><br></div><blockquote type="cite"><div><div dir="ltr">Hey guys,<div><br></div><div>I am working on a proposal which touches (and could even partially depend on) another interesting concept which I think would be a great addition for Swift.</div><div><br></div><div>Object declarations could be a way to declare a class or struct with exactly one instance. They are perfect for singletons, anonymous subclasses and to anonymously implement a protocol.</div><div><br></div><div>Here some examples how it could look like:</div><div><br></div><div><font face="monospace, monospace">// singleton</font></div><div><font face="monospace, monospace">object GlobalResource {</font></div><div><font face="monospace, monospace"> func doSomething() { … }</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">let resource = GlobalResource</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">// could all be the same</font></div><div><font face="monospace, monospace">resource.doSomething()</font></div><div><font face="monospace, monospace">GlobalResource.doSomething()</font></div><div><font face="monospace, monospace">GlobalResource.Type.doSomething()</font></div><div><br></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">// anonymous subclass</font></div><div><font face="monospace, monospace">class ViewController: UIViewController {</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> override func loadView() {</font></div><div><font face="monospace, monospace"> view = object: UIView {</font></div><div><font face="monospace, monospace"> // configure view</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">// anonymous protocol implementation</font></div><div><font face="monospace, monospace">protocol TapHandler {</font></div><div><font face="monospace, monospace"> func handleTap()</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">view.tapHandler = object: TapHandler {</font></div><div><font face="monospace, monospace"> func handleTap() {</font></div><div><font face="monospace, monospace"> // …</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><br></div><div>Kotlin is an example for a a modern language which uses this concept:</div><div><a href="https://kotlinlang.org/docs/reference/object-declarations.html">https://kotlinlang.org/docs/reference/object-declarations.html</a><br></div><div><br></div><div><br></div><div>What do you think about such an addition?</div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=1MXK54sosN3xru3iYcLt0oBZ2w20i49gyogXctgrspclYknWzz65r2qhsqfUzXb55e8dPbQJaRSSYVaFlHffez6g4VLtOrCkXCtmlLicv9OVE5R7sllNX1f7eZV1Uui5u0PCH0nK0pctWVFSkXpljT374GGF4gmc7sEVh9vBh74j4GkuPCyOD1cfUqYi9V-2Fw4inGvpUEd26VdLd6M1oQDZMdqkUjav-2FoIoGUqDI8OaY-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;">
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>