[swift-evolution] Object declarations

Marc Knaup marc at knaup.koeln
Fri Dec 11 16:43:02 CST 2015


The type is still necessary in various locations:

Generics:
let x = SomeGenericClass<SingletonObject>

Type checking:
let x: Any = SingletonObject
let x: SingletonObject? = x as? SingletonObject

Passing the type dynamically:
func foo(type: Any.Type) { … }
foo(SingletonObject)

Since there can only be one instance the "object class" (if it isn't a
struct) would be final.

On Fri, Dec 11, 2015 at 11:34 PM, Thorsten Seitz <tseitz42 at icloud.com>
wrote:

>
> Am 11.12.2015 um 23:28 schrieb Marc Knaup <marc at knaup.koeln>:
>
> Actually it's both - an object and a type. It's an object which describes
> itself.
>
>
> If it was a type then I would expect to be able to
> - derive a subclass from it
> - use it as type in variable or parameter declarations
>
> I don't think either makes sense, or does it?
>
> Btw. I have the same problem with enum cases. They're values so why are
> they uppercase?
>
>
> Hmm, good point. Guess I'm just used to it :-)
>
> -Thorsten
>
> On Fri, Dec 11, 2015 at 11:25 PM, Thorsten Seitz <tseitz42 at icloud.com>
> wrote:
>
>> 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.
>>
>> object globalResource { ... }
>>
>> -Thorsten
>>
>> Am 11.12.2015 um 23:07 schrieb Marc Knaup via swift-evolution <
>> swift-evolution at swift.org>:
>>
>> 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
>>
>>
>> What do you think about such an addition?
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/5de56d4a/attachment.html>


More information about the swift-evolution mailing list