Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Add "Singleton"/scope Protocol to make "asSingleton" default #3

Closed
mikelikespie opened this issue Jun 13, 2016 · 3 comments
Closed

Comments

@mikelikespie
Copy link
Contributor

Some classes are inherently a singleton. One may forget to do .asSingleton() when configuring them currently. If we have a protocol called singleton which behaved similar to @Singleton annotation in java when used, we can make things better. This needs to be thought out a little more though since annotations in java and protocol conformance in swift have different semantics when it comes to inheritance, etc.

@angadn
Copy link

angadn commented Jun 15, 2016

I'm curious to know if .asSingleton() affects behaviour for structs in any particular way, or is 'meaningless'

@mikelikespie
Copy link
Contributor Author

mikelikespie commented Jun 15, 2016

It will essentially memoize the value. Without a singleton the init method will be called each time it is requested. It also makes it so the injected fields of the struct are always the same.

In normal swift, one can think about it being equivalent to

static let someSingleton = SomeStruct()

Versus

 static var notASingleton: SomeStruct {
    return SomeStruct()
 }

@holmes holmes closed this as completed Jun 30, 2017
@gobetti
Copy link

gobetti commented Nov 11, 2018

Hi all (and @holmes), since this issue has been closed, I was wondering if it still makes sense to reference it in this repo's README?

From the "Scope Step" section:

In the future we may want to allow a class conforming to protocol (possibly named Singleton) to indicate that it should be bound as a singleton. It is tracked by this issue

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants