I am very much in support of adding a proper attributes DSL to Swift. 
Syntax
Syntactically I would prefer attributes to look something like
@(codable: (key: "date_of_birth", formatter: .dayFormatter))
instead of
@codable(key: "date_of_birth", formatter: .dayFormatter)
Attributes would borrow the argument-part's syntax of Swift's func-calls: …(bar: baz, blee: (qux: 42)) and thus could be arbitrarily nested.
Examples
Availability Checking
I don't think I've ever managed to get non-trivial availability checks right on first try.
Turning Swift's custom-feature #available
if #available(iOS 8.0, *) { … }
into an ordinary conditional compilation via attributes
@(if: (target: (os: "iOS", version: (greaterThanOrEqualTo: 8.1))))
class Foo {
// implementation that can use iOS 8.0+ APIs
}
would include the attributed item if the pattern matches.
More Target Checks
System and their versions are not the only things one might want to conditionally compile for:
-
@(if: (target: (arch: "x86_64"))) (only include on x86 64bit targets)
-
@(if: (target: (family: "unix"))) (only include on unix-flavored targets)
-
@(if: (target: (env: "musl"))) (only include on targets using musl instead of libc)
-
@(if: (target: (endian: "little"))) (only include on little-endian targets)
-
@(if: (target: (pointerWidth: 32))) (only include on 32bit targets)
-
@(if: (target: (vendor: "apple"))) (only include on apple targets)
-
@(if: (scheme: "test")) (only include on test executions/builds)
@(if: ())
- …
SIMD
Or take SIMD as another possible use of a composition of nested attributes:
@(if: (target_feature: "avx"))
func foo() {
// implementation that can use `avx`
}
@(if: (not: (target_feature: "avx")))
func foo() {
// a fallback implementation
}
Opinionated Examples
Being a strong proponent of explicit syntax I personally would love to see Swift take the next step and migrate lots of its current implicit specialized behaviors into attributes and by that open then up for extension to third-parties.
Synthesized Conformance
As such moving the current auto-conformance of protocols
class Foo: Equatable, Hashable { … }
to an explicit "derive" via attributes:
@(derive: (Equatable, Hashable))
class Foo { … }
Would make it clear that Equatable and Hashable are auto-derived, and not just to be found elsewhere in the code-base, when looking at a type declaration.
Conditional Synthesized Conformance
Unlike the current syntax this would also allow for expressing feature-gated conditional derivation/conformance without having to add any custom-syntax/features besides generalized attributes:
@(if: (feature: "some-feature"), then: (derive: (Equatable, Hashable)))
class Foo { … }
Enum allCases
Swift 4.2 addes support for auto-implementation of static let allCased: [Self] on enums.
This could easily be made opt-in and explicit via attributes:
protocol AllCases {
static let allCased: [Self] { get }
}
@(derive: AllCases)
enum Foo {
case Bar, Baz, Blee
}
All of this with a single generalized, yet familiar syntax.
But "why …", you wonder, "… why would we want to move all these things into attributes and make them explicit?". Because it also moves these things out of the realm of language items into ordinary userland. It makes the language leaner and yet more extensible and uniform. It makes userland extensibility a first-class citizen in Swift.
If you're thinking "wait, this looks a bit like Rust's attributes", then you're absolutely right!
Next step: Give compiler plugins a way to programmatically register custom attributes allowing for building stuff like this as a third-party.
Attributes could open so many doors for Swift: