square/Cleanse

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

mikelikespie opened this issue · 3 comments

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.

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

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()
 }

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!