AliSoftware / Dip

Simple Swift Dependency container. Use protocols to resolve your dependencies and avoid singletons / sharedInstances!
MIT License
978 stars 75 forks source link

Wrong cast lead to crash #184

Closed abesmon closed 6 years ago

abesmon commented 6 years ago

code to reproduce:

        protocol Foo {}
        class FooImpl: Foo {}

        class Bar {
            private let someFoo = Injected<Foo>()
        }

        let rootContainer = DependencyContainer { container in
            container.register(.singleton) { FooImpl() as Foo }
            try? container.bootstrap()
        }

        let aContainer = DependencyContainer { container in
            container.register(.singleton) { Bar() }
        }

        aContainer.collaborate(with: rootContainer)
        try? aContainer.bootstrap()

        let firstFoo = try? rootContainer.resolve() as Foo
        print("firstFoo is \(firstFoo)")

        let bar = try? aContainer.resolve() as Bar
        print("bar is \(bar)")

        let someFoo = try? rootContainer.resolve() as Foo
        print(someFoo)

What expected: someFoo will be same instance as firstFoo

What happens: crash and this in console:

Could not cast value of type 'Bar #1 in dipExperimentsTests.dipExperimentsTests.testDip() -> ()' (0x1011486f0) to 'dipExperimentsTests.Foo' (0x10065e490).

Also i have mentioned, that if i change FooImpl to .eagerSingleton while register, it would work as it meant to be, but i can't understand why it crashes if using that way.

ilyapuchka commented 6 years ago

have you tried latest master or latest release?

abesmon commented 6 years ago

we are using master version (6.0.0)

ilyapuchka commented 6 years ago

please try against latest master branch

abesmon commented 6 years ago

i hope i got you right. Just tried with these settings: pod 'Dip', :git => 'https://github.com/AliSoftware/Dip', :branch => 'master'

and i ran into that crash again

ilyapuchka commented 6 years ago

Sorry, I mean develop 🤦‍♂️ I just merged pr that can potentially fix it.

abesmon commented 6 years ago

ok, i'l check it

abesmon commented 6 years ago

yeah! It works 👍 we are looking forward for master version :) Thanks!

abesmon commented 6 years ago

fixed in #182