-
Notifications
You must be signed in to change notification settings - Fork 268
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
Use protocol for assembly interface? #247
Comments
I belive it's the only way to get swift working assembly interface without force casting. Since we cannot cast TyphoonDefinition into type of object buileded by definition, we cannot write code something like: public dynamic func service(runtimeText: NSString) -> MyService {
return TyphoonDefinition.withClass(MyService.self) { (definition) in
definition.injectProperty("text", with: runtimeText) as MyService
}
} We must write definitions which returns AnyObject: public dynamic func service(runtimeText: NSString) -> AnyObject {
return TyphoonDefinition.withClass(MyService.self) { (definition) in
definition.injectProperty("text", with: runtimeText)
}
} But this cause us to do casting in client code (using assembly interface): let service = self.assembly.service("runtime argument") as MyService I think it's acceptable. Not a big problem.. In objective-c we spefy object type anyway, when declaring variable MyService *service = [self.assembly serviceWith:@"runtime argument"]; So I think it's ok for Objective-C Developers. @objc protocol Assembly {
func service(runtimeText: NSString) -> MyService
} Then use this protocol for assembly injected property: var assembly: Assembly And client code becomes to: let service = self.assembly.service("runtime argument") Swift detect return types and work great. Also, as you mention, assembly protocol can be black-box documentation for those who not familiar with typhoon - clear 'factory' protocol public dynamic func appDelegate() -> AnyObject {
return TyphoonDefinition.withClass(AppDelegate.self) {
$0.injectProperty("assemblyInterface", with: self) // <-- explicit 'self' injection as assembly protocl property
}
} |
Closing. . its optional for the user. |
There are two arguments for using a protocol for the assembly interface:
Arguments against:
It would make the assembly more verbose. Objective-C style would look something like this:
File: MyAssembly.h:
File MyAssemblyImpl.m:
(perhaps no need for a MyAssemblyImpl.h, unless this seems odd)?
For Swift there's also the advantage of having a clean interface that serves as 'blackbox' documentation.
The text was updated successfully, but these errors were encountered: