|
| 1 | +# Extending `Calendar.RecurrenceRule.End` |
| 2 | + |
| 3 | + |
| 4 | +* Proposal: SF-NNNN |
| 5 | +* Author(s): Hristo Staykov <[email protected]> |
| 6 | +* Review Manager: [Tina Liu](https://github.com/itingliu) |
| 7 | +* Status: **Active review** |
| 8 | +* Bugs: <rdar://134294130> |
| 9 | +* Implementation: [apple/swift-foundation#888](https://github.com/apple/swift-foundation/pull/888) |
| 10 | +* Previous Proposal: [SF-0009](0009-calendar-recurrence-rule.md) |
| 11 | + |
| 12 | +## Revision history |
| 13 | + |
| 14 | +* **v1** Initial version |
| 15 | +* **v2** Added `CustomStringConvertible` conformance to `Calendar.RecurrenceRule.End`. |
| 16 | +* **v3** Renamed `count` to `occurrences` |
| 17 | + |
| 18 | +## Introduction |
| 19 | + |
| 20 | +In [SF-0009](0009-calendar-recurrence-rule.md) we introduced Calendar.RecurrenceRule API. In this API, we represent the end of a recurrence rule with the struct `Calendar.RecurrenceRule.End`: |
| 21 | + |
| 22 | +```swift |
| 23 | +/// When a recurring event stops recurring |
| 24 | +public struct End: Sendable, Equatable { |
| 25 | + /// The event stops repeating after a given number of times |
| 26 | + /// - Parameter count: how many times to repeat the event, including |
| 27 | + /// the first occurrence. `count` must be greater |
| 28 | + /// than `0` |
| 29 | + public static func afterOccurrences(_ count: Int) -> Self |
| 30 | + /// The event stops repeating after a given date |
| 31 | + /// - Parameter date: the date on which the event may last occur. No |
| 32 | + /// further occurrences will be found after that |
| 33 | + public static func afterDate(_ date: Date) -> Self |
| 34 | + /// The event repeats indefinitely |
| 35 | + public static var never: Self |
| 36 | + |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +This is de-facto an enum, but it was declared as struct to be future-proof. However, the original API only allowed construction of the recurrence rule end, but does not allow any introspection afterwards. This proposal adds a few properties to `Calendar.RecurrenceRule.End` to remedy this. |
| 41 | + |
| 42 | +Additionally, we add `CustomStringConvertible` conformance to the struct. Previously, implementation details would be leaked when printing (`End(_guts: Foundation.Calendar.RecurrenceRule.End.(unknown context at $1a0a00afc)._End.never)`). |
| 43 | + |
| 44 | +## Detailed design |
| 45 | + |
| 46 | +```swift |
| 47 | +public extension Calendar.RecurrenceRule.End { |
| 48 | + /// At most many times the event may occur |
| 49 | + /// This value is set when the struct was initialized with `.afterOccurrences()` |
| 50 | + @available(FoundationPreview 6.0.2, *) |
| 51 | + public var occurrences: Int? { get } |
| 52 | + |
| 53 | + /// The latest date when the event may occur |
| 54 | + /// This value is set when the struct was initialized with `.afterDate()` |
| 55 | + @available(FoundationPreview 6.0.2, *) |
| 56 | + public var date: Date? { get } |
| 57 | +} |
| 58 | + |
| 59 | +@available(FoundationPreview 6.0.2, *) |
| 60 | +public extension Calendar.RecurrenceRule.End: CustomStringConvertible { |
| 61 | + public var description: String { |
| 62 | + switch self._guts { |
| 63 | + case .never: "Never" |
| 64 | + case .afterDate(let date): "After \(date)" |
| 65 | + case .afterOccurrences(let n): "After \(n) occurrences" |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## Impact on existing code |
| 72 | + |
| 73 | +None. |
0 commit comments