[swift-evolution] Object declarations

Jérôme Duquennoy jerome+swift at duquennoy.fr
Fri Dec 11 18:58:08 CST 2015


I am working on a software in which singleton has been used in tons of places to "simplify" design.
Today, some parts of the code base are pretty impossible to test, and hard to evolve since all parts have strong links with other parts.

So I completely agree with Jordan : the singleton is a template that should be used in very specific cases and with care. I think making it a first citizen of the language by dedicating a keyword to it will not push good code design.

Jerome

> Le 12 déc. 2015 à 01:09, Riley Avron via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> a écrit :
> 
> -1. Agreed for all of Jordan's reasons.
> 
> 
> Riley
> 
> On 11 December 2015 at 14:55, Jordan Rose via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> -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).
> 
> IMHO, this just adds "another kind of thing" just to make code shorter, and "shorter" is not the right goal for Swift.
> 
> Best,
> Jordan
> 
> 
>> On Dec 11, 2015, at 14:07, Marc Knaup via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Hey guys,
>> 
>> 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.
>> 
>> 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.
>> 
>> Here some examples how it could look like:
>> 
>> // singleton
>> object GlobalResource {
>>     func doSomething() { … }
>> }
>> 
>> let resource = GlobalResource
>> 
>> // could all be the same
>> resource.doSomething()
>> GlobalResource.doSomething()
>> GlobalResource.Type.doSomething()
>> 
>> 
>> // anonymous subclass
>> class ViewController: UIViewController {
>> 
>>     override func loadView() {
>>         view = object: UIView {
>>             // configure view
>>         }
>>     }
>> }
>> 
>> 
>> // anonymous protocol implementation
>> protocol TapHandler {
>>     func handleTap()
>> }
>> 
>> view.tapHandler = object: TapHandler {
>>     func handleTap() {
>>         // …
>>     }
>> }
>> 
>> 
>> Kotlin is an example for a a modern language which uses this concept:
>> https://kotlinlang.org/docs/reference/object-declarations.html <https://kotlinlang.org/docs/reference/object-declarations.html>
>> 
>> 
>> What do you think about such an addition?
>>  _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 
>  _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151212/20de5f03/attachment.html>


More information about the swift-evolution mailing list