> On Jun 16, 2016, at 8:55 AM, Paul Cantrell via swift-evolution 
> <swift-evolution@swift.org> wrote:
> 
> Right. Is that it? Are associated types really just generic protocols + 
> single conformance constraint + type params inferred / implied?

A protocol and its associated types form a "package" or "group" of types that 
function together as one cohesive unit. For example, in the Swift world a 
collection is defined not only by its element type, but also by the type of its 
subsequence and its index. The protocol and associated types together define 
how the various types in the group relate to each other (via associated type 
nesting; for example Element belongs to Iterator belongs to Sequence), and to 
other types (via protocol conformance constraints).

A type doesn't need to be generic to adopt a protocol + associated types. For 
example, the various String views are bona fide Collections, but they're not 
generic in any way: all their associated types are fixed to concrete types as 
far as I can tell.

Another way to think of it is that generics allow *instances* to be abstracted 
over type, while protocols+associated types allow *conforming types* to be 
abstracted over type. For example, Either<T, U> can be instantiated for one 
instance as Either<String, Int>, and for another as Either<NSView, Bool>. (A 
simplified example version of the) protocol Collection can be instantiated for 
one type as being (Collection.Element = Int, Collection.Index = FooIndex), and 
another type as being (Collection.Element = String, Collection.Index = Int).

A generic type conforming to Collection would "unify" the two concepts. For 
example, SimpleArray<T> instantiates Collection as being (Collection.Element = 
T, Collection.Index = Int). Then, individual instances of SimpleArray<T> 
abstract over T by filling in T with Int (SimpleArray<Int>), String, etc.

Hope that helps,
Austin

> 
> P
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to