[swift-evolution] [Pitch] Allow nested protocol declarations
Adrian Zubarev
adrian.zubarev at devandartist.com
Fri May 6 02:30:48 CDT 2016
+1 I can’t wait for Swift 3 anymore. This ability would be great.
Additionally what do you think about something like this?
protocol DelegatableType {
protocol Delegate: class // enforced you to create a nested protocol
weak var delegate: Delegate? // or Self.Delegate?
}
class A: DelegatableType {
protocol Delegate { /* create something fancy */ }
var delegate: Delegate? // or A.Delegate? but I tend to just writing Delegate?
}
--
Adrian Zubarev
Sent with Airmail
Am 29. April 2016 bei 09:56:55, Krishna Kumar via swift-evolution (swift-evolution at swift.org) schrieb:
Hey Brad
+1 I think this will make delegate controller pattern a lot more clean and readable.
I was thinking about other areas where this can be useful, and I thought it will be great for property injection from parent controller to children.
class ParentController{
protocol Injection: class{
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
let childVC = segue.destinationViewController as? Injection
...
}
}
class ChildController: ParentController.Injection{
}
Does this make sense?
-Krishna
On Apr 28, 2016, at 10:45 PM, Brad Hilton via swift-evolution <swift-evolution at swift.org> wrote:
Type nesting allows some convenient and straightforward semantics that we see inside the Swift standard library such as views on String like String.CharacterView, String.UnicodeScalarView, etc. However a protocol cannot be nested in a type and gives a non-obvious error that the “Declaration is only valid at file scope.” Just as other nested types allow proper contextual scoping, a nested protocol could make a lot sense for a number of patterns. For example, there are many “Delegate” protocols throughout the Cocoa frameworks. Here’s a controller/delegate pattern before and after type nesting:
// Without type nesting
protocol MyControllerDelegate : class {
}
class MyController {
weak var delegate: MyControllerDelegate?
}
// With type nesting
class MyController {
weak var delegate: Delegate?
protocol Delegate : class {
}
}
Though the change is mostly semantics, it does allow an explicit association between My Controller and the Delegate instead of only a named association. It also cleans up the module name space like other nested types and makes associated protocols more discoverable in my opinion.
I’d love to hear everyone’s thoughts.
Brad Hilton
_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160506/3ffc147a/attachment.html>
More information about the swift-evolution
mailing list