Skip to content

Support for Swift Macros #80

Description

@orchetect

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions