hmlongco / Factory

A new approach to Container-Based Dependency Injection for Swift and SwiftUI.
MIT License
1.71k stars 107 forks source link

Closure factories compile ok but get tagged with errors by Xcode #89

Closed drekka closed 1 year ago

drekka commented 1 year ago

So I have the following code where I'm using Factory to return a closure instead of a type:


public func factory() -> Container {
    Container.shared
}

public typealias DateProviding = () -> Date

public extension Container {
    var dateProvider: Factory<DateProviding> { self { { Date() } } }
}

// And later in some code...
factory().dateProvider()() 

This compiles successfully however Xcode then goes an tags it with an error even though there isn't one:

Screenshot 2023-03-30 at 17 10 30

It looks likes Xcode is miss-interpreting this code even though it compiles fine. I've tried

(factory().dateProvider)()() 
(factory().dateProvider())()
let x = factory().dateProvider()
x() 

But they all trigger the error. So far it looks like the only thing that removes it is if I do this:

let x:() -> Date = factory().dateProvider()
x() 

Any thoughts on how to get Xcode to behave?

hmlongco commented 1 year ago

I dropped this into the factory demo and ran it with no issues.

public func factory() -> Container {
    Container.shared
}

public typealias DateProviding = () -> Date

public extension Container {
    var dateProvider: Factory<DateProviding> { self { { Date() } } }
}

func test() {
    // And later in some code...
    let date = factory().dateProvider()()
    print(date)
}

The problem is probably somewhere in the code you didn't post. You're masking factory or Container with some locally defined name.

drekka commented 1 year ago

Hmm. Ok. Let me dig into that bit more :-)

drekka commented 1 year ago

Ok, my bad. Looks like some subtle missing imports and missing dependency references in a package file. The compiler was still able to find everything through transient dependencies and the likes, but XCode was flagging the errors when it was compiling for code completion. Which explains why it would compile and run fine, then a few seconds later Xcode would flag an error. Anyway, adding the imports and missing dependencies and it all appears to be good now.

Thanks for taking the time to double check :-)