Skip to content

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

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 12, 2022

Conversation

lorentey
Copy link
Member

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
@lorentey
Copy link
Member Author

@swift-ci test

@lorentey lorentey requested review from phausler and rjmccall April 11, 2022 22:17
@lorentey
Copy link
Member Author

We could also update references to Instant.Duration elsewhere in the protocol & its extensions. Do we want to do that?

var minimumResolution: Instant.Duration { get }
  @available(SwiftStdlib 5.7, *)
  public func measure(_ work: () throws -> Void) rethrows -> Instant.Duration {

@phausler
Copy link
Contributor

Since the code-hints will complete to a potentially ambiguous type it likely is good to just leave them as Instant.Duration (plus it is less work to do).

Copy link
Contributor

@phausler phausler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@lorentey
Copy link
Member Author

swiftlang/swift-evolution#1618 is merged now; this looks OK to land.

@lorentey lorentey merged commit d366fe5 into swiftlang:main Apr 12, 2022
@lorentey lorentey deleted the clock-duration branch April 12, 2022 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants