Patterns for Working With Associated Types

Associated Types and Self

released Fri, 01 Mar 2019
Swift Version 5.0

Associated Types and Self

Another vector which can introduce associated types into your codebase is the usage of Self:

protocol Example {
  /// Indirect Associated Type
  var builder: Self { get }
  /// Indirect Associated Type
  func makeSomething(with example: Self)
}
var myExamples: [Example] = []

As you can see in the example above, using Self as a method parameter or using Self as a property type automatically introduces an associated type (like we saw with Equatable, earlier).

The most helpful note here is that once you use a method instead of a property in order to return something of type Self you will not opt in to an associated type:

protocol Example {
  /// No Indirect Associated Type
  func builder() -> Self
}
var myExamples: [Example] = []

This example works fine. No indirect associated type is introduced.