time icon indicating copy to clipboard operation
time copied to clipboard

Add Swift 5.5 syntax sugar for static extensions on Clock

Open Mordil opened this issue 3 years ago • 3 comments

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))

Mordil avatar Oct 20 '21 05:10 Mordil

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 :(

Mordil avatar Oct 20 '21 05:10 Mordil

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.

davedelong avatar Oct 23 '21 19:10 davedelong

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

Mordil avatar Oct 23 '21 23:10 Mordil

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.

davedelong avatar Feb 29 '24 17:02 davedelong