Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Swift Macros #80

Open
orchetect opened this issue Oct 8, 2024 · 0 comments
Open

Support for Swift Macros #80

orchetect opened this issue Oct 8, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@orchetect
Copy link
Owner

orchetect commented Oct 8, 2024

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:

  • using timecode string literals or using component value literals, and
  • using frame rate literal

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

  • This would allow for less friction when using timecode literals, and would provide a safer alternative to the temptation to use try! with known-valid timecode literals.
@orchetect orchetect added the enhancement New feature or request label Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant