# \[Pitch\] Primary Associated Types in the Standard Library

**URL:** <https://forums.swift.org/t/pitch-primary-associated-types-in-the-standard-library/56426>\
**Category:** Pitches\
**Tags:** generics, protocols, stdlib\
**Created:** [April 1, 2022, 12:31am UTC](https://forums.swift.org/t/pitch-primary-associated-types-in-the-standard-library/56426 "2022-04-01T00:31:19Z")\
**Posts on this page:** 1\
**Showing post:** 30

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [April 6, 2022, 10:31pm UTC](https://forums.swift.org/t/pitch-primary-associated-types-in-the-standard-library/56426/30 "2022-04-06T22:31:24Z")

</div>

> [@Philippe\_Hausler](#):
>
> Clock does not have a primary associated type imho. Clocks are used upon their behavior not associated types.

Interacting with a clock seems to require knowledge of its `Instant.Duration`, right? Otherwise you have no way of referring to an advanced instant, or how to tell it to `sleep` to a certain instant.

So it sounds appropriate for `Clock`'s primary associated type to be this associated type of its `Instant`. Similar to how `Collection<Element>` shares its primary associated type with its `Iterator`.

Having `Clock<Duration>` would allow you to tell an arbitrary clock to sleep so long as you know its primary associated type, which is particularly useful for writing testable, time-based code, where your application logic can be injected with `some Clock<Duration>`: it can take a `ContinuousClock` when run in release, and a theoretical `TestClock` that can be manually advanced in your tests.

In [Combine Schedulers](https://github.com/pointfreeco/combine-schedulers) we needed to parameterize a type-erased scheduler ([`AnyScheduler`](https://github.com/pointfreeco/combine-schedulers/blob/84858ad114d8737af5fe58d726d81f15d5b8bd23/Sources/CombineSchedulers/AnyScheduler.swift#L121)) and [`TestScheduler`](https://github.com/pointfreeco/combine-schedulers/blob/84858ad114d8737af5fe58d726d81f15d5b8bd23/Sources/CombineSchedulers/TestScheduler.swift#L58) over the `SchedulerTimeType` in order to inject testable schedulers into application code. It would seem that without a primary associated type on `Clock`, anyone that would want to do something similar would need to still resort to bespoke wrapper types, like `AnyClock`.

---

_[View the full topic](https://forums.swift.org/t/pitch-primary-associated-types-in-the-standard-library/56426)._
