Difference between declaring @Dependency inside func and inside class/struct? #410
Replies: 1 comment
-
|
Hi @gerchicov-vg, it's ok to do this in the sense that it compiles and will most likely do what you want. But there is a difference between: class Model {
@Dependency(\.client) var client
func tap() { client.perform() }
}…and: class Model {
func tap() {
@Dependency(\.client) var client
client.perform()
}
}In the first example, the dependency resolves the value of The vast majority of dependencies are only ever set a single time at the entry point of the app, and for such dependencies there is no distinction between these two styles. But, the first style is typically easier to understand, and we would recommend it, but also understand if you are in the process of refactoring a large, complex app, you may not be able to do that always. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
According to examples you suggest to use this macro for inner class/struct variables only. But some people use it to declare local variables inside func directly. Is it correct?
And is it correct for example to replace
SomeSingleton.shared.someFunc()with the following code:Beta Was this translation helpful? Give feedback.
All reactions