Skip to content

[5.7][stdlib] Add Clock.Duration as an associated type requirement #42316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2022

Conversation

lorentey
Copy link
Member

(cherry picked from #42314, commit 0aadfb0)

As discussed on the forum, we'll likely want to use Duration as the primary associated type on protocol Clock; however, that protocol currently only has Instant.

To support declaring it as the primary associated type, protocol Clock needs to have Duration as an associated type requirement:

@available(SwiftStdlib 5.7, *)
public protocol Clock: Sendable {
  associatedtype Duration: DurationProtocol
  associatedtype Instant: InstantProtocol where Instant.Duration == Duration

  var now: Instant { get }
  var minimumResolution: Instant.Duration { get }

  func sleep(until deadline: Instant, tolerance: Instant.Duration?) async throws
}

This setup is reminiscent of Sequence.Element vs. Sequence.Iterator.Element.

SE-0329 has not yet shipped in an ABI stable release, so we still have the opportunity to address this.

rdar://91591545

As [discussed on the forum][forum], we'll likely want to use
`Duration` as the primary associated type on `protocol Clock`;
however, that protocol currently only has `Instant`.

[forum]: https://forums.swift.org/t/pitch-primary-associated-types-in-the-standard-library/56426/39

To support declaring it as the primary associated type,
`protocol Clock` needs to have `Duration` as an associated type
requirement:

```
@available(SwiftStdlib 5.7, *)
public protocol Clock: Sendable {
  associatedtype Duration: DurationProtocol
  associatedtype Instant: InstantProtocol where Instant.Duration == Duration

  var now: Instant { get }
  var minimumResolution: Instant.Duration { get }

  func sleep(until deadline: Instant, tolerance: Instant.Duration?) async throws
}
```

This setup is reminiscent of `Sequence.Element` vs.
`Sequence.Iterator.Element`.

SE-0329 has not yet shipped in an ABI stable release, so we still have
the opportunity to address this.

rdar://91591545
(cherry picked from commit 0aadfb0)
@lorentey lorentey added the swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review label Apr 11, 2022
@lorentey lorentey requested a review from a team as a code owner April 11, 2022 22:30
@lorentey
Copy link
Member Author

lorentey commented Apr 11, 2022

This is blocked on the corresponding amendment to SE-0329 landing in Swift Evolution (swiftlang/swift-evolution#1618).

@lorentey
Copy link
Member Author

@swift-ci test

@lorentey lorentey added swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process and removed swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review labels Apr 12, 2022
@lorentey lorentey requested a review from stephentyrone April 12, 2022 21:54
@lorentey
Copy link
Member Author

(swiftlang/swift-evolution#1618 is merged now)

@lorentey lorentey requested a review from rjmccall April 19, 2022 22:51
@lorentey lorentey merged commit 59e61d8 into swiftlang:release/5.7 Apr 21, 2022
@lorentey lorentey deleted the clock-duration-5.7 branch April 21, 2022 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants