GithubHelp home page GithubHelp logo

Comments (5)

hmlongco avatar hmlongco commented on August 26, 2024 1

You can (at the moment), also sneak in and change the internal key yourself.

    public var sharedClass: Factory<SharedClass> {
        self(key: "sharedClassB") { .init() }.singleton
    }

from factory.

hmlongco avatar hmlongco commented on August 26, 2024

So yes. You're creating the same class with the same name in multiple modules with a singleton scope. Singletons use a mashup of the factory function name and the type name as cache key. So basically you're using the same identical function name AND the same identical class name--even though they're actually different types--in different modules. So the name is found is found in the singleton cache but the actual type is different so it's recreated and recached.

I'll look to see what I can do, but as discussed in the documentation singletons are somewhat problematic to use in your code. Given your example of simply using the shared container instance, you'd be better off simply specifying the scope as .cached.

See the section on singletons in the Testing documentation.

from factory.

deivitaka avatar deivitaka commented on August 26, 2024

Thanks for the info. I failed to notice the function name also affects the cache key, which would also be acceptable to change.

Given my scenario, I have a wrapper for UserDefaults with the same name / functionality, but still would be unique singletons with different properties. So would be nice to consider the module name also in the cache key. Also in cases of third party packages that use Factory (in the rare occasion they may collide).

I would close this, since there is a workaround / preferred usage.

from factory.

hmlongco avatar hmlongco commented on August 26, 2024

Will be fixed in 2.1.4

from factory.

hmlongco avatar hmlongco commented on August 26, 2024

Fixed.

from factory.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.