wilshipley
(Wil Shipley)
December 2, 2021, 11:37pm
1
Recently I was looking at some Apple sample code that uses Date
and it had this:
let oneMinute: TimeInterval = 60
var currentDate = Date()
var entries: [SimpleEntry] = []
while currentDate < endDate {
let relevance = TimelineEntryRelevance(score: Float(selectedCharacter.healthLevel))
let entry = SimpleEntry(date: currentDate, relevance: relevance, character: selectedCharacter)
currentDate += oneMinute
entries.append(entry)
}
Which I rewrote as this:
let entries: [SimpleEntry] = stride(from: Date(), to: selectedCharacter.fullHealthDate, by: 60.0).map {
SimpleEntry(date: $0, character: selectedCharacter, relevance: TimelineEntryRelevance(score: Float(selectedCharacter.healthLevel)))
}
Which I liked a lot better, but it only worked because I added this category:
extension Date : Strideable {
public dynamic func distance(to other: Self) -> TimeInterval {
other.timeIntervalSinceReferenceDate - self.timeIntervalSinceReferenceDate
}
public dynamic func advanced(by n: TimeInterval) -> Self {
Date(timeIntervalSinceReferenceDate: self.timeIntervalSinceReferenceDate + n)
}
}
So, I thought I'd ask, can Date
be made Strideable
in the Swift stdlib? Because it sure seems neat to me.
I know there are efforts afoot to redo time and stuff, but since Date
has been ingrained into the system for so long I figure we might as well make it easier to deal with
-Wil
1 Like
xwu
(Xiaodi Wu)
December 2, 2021, 11:54pm
2
Date
is a part of Foundation and not the standard library, with the consequence that it’s not subject to the Swift Evolution process.
As of iOS 13, Apple added conformance of Date
to Strideable
:
iOS 13 shows that Foundation.Date now conforms to the Strideable protocol.
This proposal came up before (over a year ago!), but never went through review (that I'm aware of).
Did I miss this review? If so, can someone please link me to it, because my attempts to search for it have been unsuccessful
If I did not miss this review, then in which beta can we expect this change to be removed, and when can we expect the review for this proposal to happen?
In trying to compile our code with XC 11 we ran into an ambiguous function error with distance(to:) since we created our own conformance to Strideable on Foundation.Date. It looks like in iOS 13 and above Foundation also added a conformance to Strideable on Date which is causing the ambiguous function issue.
We could remove the conformance, but since we support back to iOS 10.0 we would have to reimplement all the same strideable functions. We tried using the obsoleted macro but that didn't wor…
wilshipley
(Wil Shipley)
December 2, 2021, 11:56pm
3
That's odd, on my machine I was getting "Date isn't Strideable" errors. Hrm.
scanon
(Steve Canon)
December 3, 2021, 12:17am
4
What deployment target were you using?
Foundation.Date
has all the requirements of Strideable
, but it doesn't conform yet: FB9452917.
The following changes would be needed:
move the existing availability (iOS 13, etc.) to each individual requirement.
add the new availability (iOS 9999 == Next) and the protocol conformance.
-@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
-extension Date {
+@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
+extension Date: Strideable {
+ @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public typealias Stride = TimeInterval
+ @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public func distance(to: Date) -> TimeInterval
+ @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public func advanced(by: TimeInterval) -> Date
}
xwu
(Xiaodi Wu)
December 4, 2021, 6:23am
7
Huh, I guess I stand corrected.
Wouldn’t the only change needed be as follows:
+@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
+extension Date: Strideable { }
1 Like
That should work too, but it might be unusual to have the requirements in one extension, and the conformance in another (empty) extension.
The style guide for the standard library doesn't allow inheritance of availability, but Foundation has a different style guide.
Conformance availability was implemented in Swift 5.3 or 5.4, so there should probably be an audit of Foundation (and the standard library) to see which other conformances can be added.