<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=""><div class="">-1. Enforced singletons lead to code that can't be unit-tested. Local types are occasionally convenient and they can't be unit-tested either, but spending one extra line to name the thing isn't going to hurt. You also can't pick an initializer here (at least not in this version).</div><div class=""><br class=""></div><div class="">IMHO, this just adds "another kind of thing" just to make code shorter, and "shorter" is not the right goal for Swift.</div><div class=""><br class=""></div><div class="">Best,</div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 11, 2015, at 14:07, Marc Knaup via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hey guys,<div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">Here some examples how it could look like:</div><div class=""><br class=""></div><div class=""><font face="monospace, monospace" class="">// singleton</font></div><div class=""><font face="monospace, monospace" class="">object GlobalResource {</font></div><div class=""><font face="monospace, monospace" class=""> func doSomething() { … }</font></div><div class=""><font face="monospace, monospace" class="">}</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">let resource = GlobalResource</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">// could all be the same</font></div><div class=""><font face="monospace, monospace" class="">resource.doSomething()</font></div><div class=""><font face="monospace, monospace" class="">GlobalResource.doSomething()</font></div><div class=""><font face="monospace, monospace" class="">GlobalResource.Type.doSomething()</font></div><div class=""><br class=""></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">// anonymous subclass</font></div><div class=""><font face="monospace, monospace" class="">class ViewController: UIViewController {</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class=""> override func loadView() {</font></div><div class=""><font face="monospace, monospace" class=""> view = object: UIView {</font></div><div class=""><font face="monospace, monospace" class=""> // configure view</font></div><div class=""><font face="monospace, monospace" class=""> }</font></div><div class=""><font face="monospace, monospace" class=""> }</font></div><div class=""><font face="monospace, monospace" class="">}</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">// anonymous protocol implementation</font></div><div class=""><font face="monospace, monospace" class="">protocol TapHandler {</font></div><div class=""><font face="monospace, monospace" class=""> func handleTap()</font></div><div class=""><font face="monospace, monospace" class="">}</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">view.tapHandler = object: TapHandler {</font></div><div class=""><font face="monospace, monospace" class=""> func handleTap() {</font></div><div class=""><font face="monospace, monospace" class=""> // …</font></div><div class=""><font face="monospace, monospace" class=""> }</font></div><div class=""><font face="monospace, monospace" class="">}</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><br class=""></div><div class="">Kotlin is an example for a a modern language which uses this concept:</div><div class=""><a href="https://kotlinlang.org/docs/reference/object-declarations.html" class="">https://kotlinlang.org/docs/reference/object-declarations.html</a><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">What do you think about such an addition?</div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=ZEz4qHYnXhPr3bBPu-2FxP4tN3HfWKL-2FtJpqkQ0gkOVSAWVUCgq6j2gIMHLwUtsMvkh6zS-2F42OEDmC-2FuP8IhnJLSUcx5onHlpzOIn-2Frg3W6-2FKCm-2BXK-2FwGfUgVX09VHJVmk0TZUSq1TJuaeeSv-2FaBPGDu-2B53-2Bqq59Y9-2B5QQfYQCKUU-2Ft-2FjzKmkw6Hagd6EvybiQEVi8zW1ZX8PM26dnKJqSktSHIVAl3j0WqiS-2BX-2BGJKow-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;" class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>