AliSoftware/Dip

Wrong cast lead to crash

abesmon opened this issue · 8 comments

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.

have you tried latest master or latest release?

we are using master version (6.0.0)

please try against latest master branch

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

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

ok, i'l check it

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

fixed in #182