Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about using closure type constants in Dependency #65

Open
kosukeohmura opened this issue Jan 2, 2019 · 0 comments
Open

Question about using closure type constants in Dependency #65

kosukeohmura opened this issue Jan 2, 2019 · 0 comments

Comments

@kosukeohmura
Copy link

When I define a class that conforms to Injectable and has a closure in its Dependency like below:

class SomeModel: Injectable {
    struct Dependency {
        let stringFactory: (Int) -> String
    }

    let stringFactory: (Int) -> String

    required init(dependency: Dependency) {
        self.stringFactory = dependency.stringFactory
    }
}

Then, I run dikitgen and I get error below. Closures in function parameter need @escaping.

    // Passing non-escaping parameter 'stringFactory' to function expecting an @escaping closure
    // Parameter 'stringFactory' is implicitly non-escaping
    func resolveSomeModel(stringFactory: (Int) -> String) -> SomeModel {
        return SomeModel(dependency: .init(stringFactory: stringFactory))
    }

I can avoid this error by creating factory container like below:

struct StringFactoryContainer {
    let factory:  (Int) -> String
}

And use like this:

class SomeModel: Injectable {
    struct Dependency {
        let stringFactoryContainer: StringFactoryContainer
    }

    let stringFactory: (Int) -> String

    required init(dependency: Dependency) {
        self.stringFactory = dependency.stringFactoryContainer.factory
    }
}

Does anyone know good way to use closure in Dependency?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant