Hey
I am trying to infer the type of an object's property from the type of another object's member. To give you some context, please take a look at the following valid Swift code:
Code snippet 1
final class NetworkService {
func fetch(completion: @escaping (Result<Bool, Error>) -> Void) {
completion(.success(true))
}
}
struct Dependencies {
let fetch: (@escaping (Result<Bool, Error>) -> Void) -> Void
}
final class Repository {
private let dependencies: Dependencies
init(dependencies: Dependencies) {
self.dependencies = dependencies
}
}
let repository = Repository(dependencies: .init(fetch: NetworkService().fetch))
I would like to do something that is currently not possible (not valid swift at the moment, but helps to show what I would like to do):
Code snippet 2
struct Dependencies {
let fetch: type(of: NetworkService.fetch)
}
As a comparison, the code below is valid Swift:
Code snippet 3
func setType<T, Input, Output>(of function: (T) -> (Input) -> Output) -> (Input) -> Output {
fatalError()
}
...
struct Dependencies {
var fetch = setType(of: NetworkService.fetch)
}
...
let repository = Repository(dependencies: .init(fetch: NetworkService().fetch))
So I have the following doubts:
- What is the right naming convention to refer to the approach shown in code snippet 2? Maybe
type inference at type annotation level
? - I could not find much documentation about the approach shown in code snippet 2 (maybe because I dont even know how to name it in the first place, so it is difficult to search for it ) . Could any of you reference any?
- Considering the valid code from code snippet 3, do you think that we as developers should be able to write something like code snippet 2?
- Is there any interest on allowing an approach as the one shown in code snippet 2?
Thank you very much