Skip to content

Commit 43fb0a5

Browse files
committed
Add ChangeLog entry for macros
1 parent f1cdbe7 commit 43fb0a5

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

CHANGELOG.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,69 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
44

55
## Swift 5.9
66

7+
* [SE-0382][], [SE-0389][], [SE-0394][], [SE-0397][]:
8+
9+
Swift 5.9 includes a new macro system that can be used to eliminate boilerplate and provide new forms of expressive APIs. Macros are declared with the new `macro` introducer:
10+
11+
```swift
12+
@freestanding(expression)
13+
macro assert(_ condition: Bool) = #externalMacro(module: "PowerAssertMacros", type: "AssertMacro")
14+
```
15+
16+
Macros have parameter and result types, like functions, but are defined as separate programs that operate on syntax trees (using [swift-syntax][]) and produce new syntax trees that are incorporated into the program. Freestanding macros, indicated with the `@freestanding` attribute, are expanded in source code with a leading `#`:
17+
18+
```swift
19+
#assert(x + y == z) // expands to check the result of x + y == z and report failure if it's false
20+
```
21+
22+
Macros can also be marked as `@attached`, in which case they will be meaning that they will be expanded using custom attribute syntax. For example:
23+
24+
```swift
25+
@attached(peer, names: overloaded)
26+
macro AddCompletionHandler() = #externalMacro(
27+
module: "ConcurrencyHelperMacros",
28+
type: "AddCompletionHandlerMacro"
29+
)
30+
31+
@AddCompletionHandler
32+
func fetchAvatar(from url: URL) throws -> Image { ... }
33+
34+
// expands to...
35+
func fetchAvatar(from url: URL, completionHandler: @escaping (Result<Image, Error>) -> Void) {
36+
Task.detached {
37+
do {
38+
let result = try await fetchAvatar(from: url)
39+
completionHandler(.success(result))
40+
} catch {
41+
completionHandler(.failure(error))
42+
}
43+
}
44+
}
45+
```
46+
47+
Macros are implemented in separate programs, which are executed by the Swift compiler. The Swift Package Manager's manifest provides a new `macro` target type to describe macros:
48+
49+
```swift
50+
import PackageDescription
51+
import CompilerPluginSupport
52+
53+
let package = Package(
54+
name: "ConcurrencyHelpers",
55+
dependencies: [
56+
.package(url: "https://github.com/apple/swift-syntax", from: "509.0.0"),
57+
],
58+
targets: [
59+
.macro(name: "ConcurrencyHelperMacros",
60+
dependencies: [
61+
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
62+
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
63+
]),
64+
.target(name: "ConcurrencyHelpers", dependencies: ["ConcurrencyHelperMacros"]),
65+
.testTarget(name: "ConcurrencyHelperMacroTests", dependencies: ["ConcurrencyHelperMacros"]),
66+
]
67+
)
68+
```
69+
770
* [SE-0380][]:
871

972
`if` and `switch` statements may now be used as expressions to:
@@ -9763,7 +9826,10 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
97639826
[SE-0376]: <https://github.com/apple/swift-evolution/blob/main/proposals/0376-function-back-deployment.md>
97649827
[SE-0377]: <https://github.com/apple/swift-evolution/blob/main/proposals/0377-parameter-ownership-modifiers.md>
97659828
[SE-0380]: <https://github.com/apple/swift-evolution/blob/main/proposals/0380-if-switch-expressions.md>
9766-
9829+
[SE-0382]: https://github.com/apple/swift-evolution/blob/main/proposals/0382-expression-macros.md
9830+
[SE-0389]: https://github.com/apple/swift-evolution/blob/main/proposals/0389-attached-macros.md
9831+
[SE-0394]: https://github.com/apple/swift-evolution/blob/main/proposals/0394-swiftpm-expression-macros.md
9832+
[SE-0397]: https://github.com/apple/swift-evolution/blob/main/proposals/0397-freestanding-declaration-macros.md
97679833
[#64927]: <https://github.com/apple/swift/issues/64927>
97689834
[#42697]: <https://github.com/apple/swift/issues/42697>
97699835
[#42728]: <https://github.com/apple/swift/issues/42728>
@@ -9805,3 +9871,4 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
98059871
[#57081]: <https://github.com/apple/swift/issues/57081>
98069872
[#57225]: <https://github.com/apple/swift/issues/57225>
98079873
[#56139]: <https://github.com/apple/swift/issues/56139>
9874+
[swift-syntax]: https://github.com/apple/swift-syntax

0 commit comments

Comments
 (0)