orchetect / TimecodeKit

The definitive SMPTE timecode library for Swift.
MIT License
91 stars 8 forks source link

Support for Swift Macros #80

Open orchetect opened 1 month ago

orchetect commented 1 month ago

Brief

Swift Macros can bring some novel and useful functionality.

Since macros can run code at compile time, it's possible to use them to create non-throwing Timecode constructors when:

This would allow for timecode validation checking at compile-time for timecode literals as described above.

Proposal

Take for example, a basic Timecode initializer that takes a String. Currently, this is throwing and requires the try keyword since at compile-time it is unknown whether the timecode string is a valid timecode at the given frame rate or not.

let timecode = try Timecode(.string("01:02:30:15"), at: .fps24)

For example, a hypothetical macro could work as follows:

let timecode = #timecode(.string("01:02:30:15"), at: .fps24)
// or even simplified to:
let timecode = #timecode("01:02:30:15", at: .fps24)

This macro could be evaluated at compile-time and be determined to be valid timecode, removing the requirement to use the try keyword. If the string is invalid timecode, a compiler error would be produced.

Benefits