time
time copied to clipboard
Add Swift 5.5 syntax sugar for static extensions on Clock
Motivation
When determining which Clock
value to use, it can be less expressive or easily forgotten to use the Clocks
namespace to access the several built-in values.
In Swift 5.5 we gain the ability to provide a leading dot syntax, even when dealing in a protocol context where the concrete type isn't (yet) known.
Changes Made
When developers are building the library with a 5.5 compiler or later, new static extensions on Clock
will be available that provide the same values as found on Clocks
.
This required making some properties and types public
, and changing their names to have a _
prefix to hide them in autocompletion. Their initializers are still internal
to disallow outside creation of instances.
Results
Developers will now be able to write the following code:
func someClockContext(_ clock: Clock) { /* ... */ }
someClockContext(.system)
someClockContext(.custom(startingFrom: Clocks.system.thisInstant(), rate: 2.0))
This also includes a commit to rename the folders to no longer contain spaces, as it seems the Swift 5.5 compiler doesn't like that anymore :(
I like where this is going, but I'm hesitant to make major changes to any of the Clock stuff while the Swift Evolution pitch is ongoing.
I’m assuming you’re referring to the STD Library pitch for some of these “currency” types? (Clock, Instant, etc.)
That’s a fair request
I'm going to reject this for now, since it would involve exposing internal types, and I'm not convinced the demand exists for this functionality. We can absolutely re-evaluate this as we gather more usage feedback.