[swift-evolution] [Pitch] Allow nested protocol declarations

Kurt Werle kurt at circlew.org
Thu Apr 28 21:22:05 CDT 2016


As a ruby programmer, I have to admit that I've tried to do this in Swift
several times before remembering again that it isn't possible.
+1

On Thu, Apr 28, 2016 at 3:44 PM, Howard Lovatt via swift-evolution <
swift-evolution at swift.org> wrote:

> +1 very handy to associate types together
>
>
> On Friday, 29 April 2016, Michael Peternell via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>> I think that would be a good feature.
>>
>> > Am 28.04.2016 um 19:15 schrieb Brad Hilton via swift-evolution <
>> swift-evolution at swift.org>:
>> >
>> > 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
>>
>
>
> --
> -- Howard.
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>


-- 
kurt at CircleW.org
http://www.CircleW.org/kurt/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160428/b86de21e/attachment.html>


More information about the swift-evolution mailing list