[swift-users] Associatedtype Naming Conventions

Slava Pestov spestov at apple.com
Wed May 31 18:26:52 CDT 2017


> On May 31, 2017, at 4:16 PM, Steven Brunwasser <sbrunwasser at gmail.com> wrote:
> 
> Basically, my library contains a bunch of collection-like protocols, which can be combined in different ways and can be compatible with each other in certain combinations when the proper types align. Naturally, they all have an Element associatedtype, but I need to allow for the possibility where two of these collection protocols are implemented in the same structure that requires different type definitions for each protocols' Element.

Oh, I understand now. This is intentionally not supported — associated types with the same name from different protocols must all be implemented by the same typealias in the conforming type.

Slava

> 
> I’ve been trying to make some other protocols to simplify some definitions, but specifying a parent protocol’s associated type within a child protocol doesn’t seem to work.
> 
> 	protocol Buzz: Bar {
> 		typealias Container = A
> 	}
> 
> 	struct BuzzImpl: Buzz {} // error: type ‘BuzzImpl' does not conform to protocol ‘Buzz'
> 
> On May 31, 2017 at 4:02:43 PM, Slava Pestov (spestov at apple.com <mailto:spestov at apple.com>) wrote:
> 
>> Can you give an example of a problematic name collision? Does fully qualifying names not help?
>> 
>> Slava
>> 
>>> On May 31, 2017, at 4:01 PM, Steven Brunwasser via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>> 
>>> Hi,
>>> 
>>> I have a library which uses a few generic protocols with identically named associated types that may not always be specified identically by implementors.
>>> 
>>> protocol Foo {
>>> associatedtype Container
>>> associatedtype Element
>>> }
>>> 
>>> protocol Bar {
>>> associatedtype Container
>>> associatedtype Element
>>> }
>>> 
>>> struct Baz: Foo, Bar {
>>> // Implement using two different Container/Element types.
>>> }
>>> 
>>> Is there a consensus on some naming convention for associatedtypes to mitigate name collisions?
>>> Would it be acceptable to add namespace prefixes to these types?
>>> 
>>> protocol Foo {
>>> associatedtype FooContainer
>>> associatedtype FooElement
>>> }
>>> 
>>> I’m using the dictionary and thesaurus to find some alternative names I could use, but the ones already used are so the most sensical semantically.
>>> 
>>> Do you have any suggestions?
>>> 
>>> Thanks, 
>>> - Steve Brunwasser
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users at swift.org <mailto:swift-users at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170531/1f8941e8/attachment.html>


More information about the swift-users mailing list