Skip to content

Commit 5b619ba

Browse files
committed
translate first 3 sections
1 parent 5e309b1 commit 5b619ba

File tree

1 file changed

+25
-86
lines changed

1 file changed

+25
-86
lines changed

swift-6-beta.docc/LanguageGuide/Macros.md

Lines changed: 25 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,26 @@
1-
<!--
2-
要翻译的文件:https://github.com/SwiftGGTeam/the-swift-programming-language-in-chinese/blob/swift-6-beta-translation/swift-6-beta.docc/LanguageGuide/Macros.md
3-
Swift 文档源文件地址:https://docs.swift.org/swift-book/documentation/the-swift-programming-language/macros
4-
翻译估计用时:⭐️⭐️⭐️⭐️⭐️
5-
-->
6-
7-
# Macros
1+
#
82

9-
Use macros to generate code at compile time.
3+
在编译时使用宏生成代码。
104

11-
Macros transform your source code when you compile it,
12-
letting you avoid writing repetitive code by hand.
13-
During compilation,
14-
Swift expands any macros in your code before building your code as usual.
5+
宏会在你编译你的源代码时对其进行转换,从而可以让你避免手动编写重复的代码。在编译过程中,Swift 会先展开代码中的所有宏,然后再像往常一样构建代码。
156

16-
![A diagram showing an overview of macro expansion. On the left, a stylized representation of Swift code. On the right, the same code with several lines added by the macro.](macro-expansion)
7+
![一个显示宏展开概貌的图表。左侧是 Swift 代码的风格化表示。右侧是由宏添加了几行的相同的代码。](macro-expansion)
178

18-
Expanding a macro is always an additive operation:
19-
Macros add new code,
20-
but they never delete or modify existing code.
9+
宏展开始终是一种加法操作:宏会添加新代码,但绝不会删除或修改现有代码。
2110

22-
Both the input to a macro and the output of macro expansion
23-
are checked to ensure they're syntactically valid Swift code.
24-
Likewise, the values you pass to a macro
25-
and the values in code generated by a macro
26-
are checked to ensure they have the correct types.
27-
In addition,
28-
if the macro's implementation encounters an error when expanding that macro,
29-
the compiler treats this as a compilation error.
30-
These guarantees make it easier to reason about code that uses macros,
31-
and they make it easier to identify issues
32-
like using a macro incorrectly
33-
or a macro implementation that has a bug.
11+
每个宏的输入和宏展开的输出都会被检查,以确保它们是语法上有效的 Swift 代码。同样,你传给宏的值以及宏生成的代码中的值也会被检查,以确保它们具有正确的类型。此外,如果宏的实现在扩展宏时遇到错误,编译器会将其视为编译错误。这些保证让使用了宏的代码更容易被推导,也更容易发现诸如宏使用不当或宏实现中的错误这样的问题。
3412

35-
Swift has two kinds of macros:
13+
Swift 有两种宏:
3614

37-
- *Freestanding macros* appear on their own,
38-
without being attached to a declaration.
15+
- *独立宏*可单独出现,无需被附加到任何声明中。
3916

40-
- *Attached macros* modify the declaration that they're attached to.
17+
- *附加宏*会修改它被附加到的声明。
4118

42-
You call attached and freestanding macros slightly differently,
43-
but they both follow the same model for macro expansion,
44-
and you implement them both using the same approach.
45-
The following sections describe both kinds of macros in more detail.
19+
附加宏和独立宏的调用方式略有不同,但它们都遵循相同的宏展开模型,并都使用相同的方法来实现。下面的章节将更详细地描述这两种宏。
4620

47-
## Freestanding Macros
21+
## 独立宏
4822

49-
To call a freestanding macro,
50-
you write a number sign (`#`) before its name,
51-
and you write any arguments to the macro in parentheses after its name.
52-
For example:
23+
要调用独立宏,需要在其名称前写入数字符号 (`#`),并在名称后的括号中写入宏的参数。例如:
5324

5425
```swift
5526
func myFunction() {
@@ -58,36 +29,21 @@ func myFunction() {
5829
}
5930
```
6031

61-
In the first line,
62-
`#function` calls the [`function()`][] macro from the Swift standard library.
63-
When you compile this code,
64-
Swift calls that macro's implementation,
65-
which replaces `#function` with the name of the current function.
66-
When you run this code and call `myFunction()`,
67-
it prints "Currently running myFunction()".
68-
In the second line,
69-
`#warning` calls the [`warning(_:)`][] macro from the Swift standard library
70-
to produce a custom compile-time warning.
32+
在函数内的第一行中,`#function` 调用了 Swift 标准库中的 [`function()`][] 宏。当你编译此代码时,Swift 会调用该宏的实现,将 `#function` 替换为当前函数的名称。当你运行这段代码并调用 `myFunction()` 时,它会打印 “Currently running myFunction()”。在第二行中,`#warning` 调用了 Swift 标准库中的 [`warning(_:)`][] 宏,以生成一个自定义的编译时警告。
7133

7234
[`function()`]: https://developer.apple.com/documentation/swift/function()
7335
[`warning(_:)`]: https://developer.apple.com/documentation/swift/warning(_:)
7436

75-
Freestanding macros can produce a value, like `#function` does,
76-
or they can perform an action at compile time, like `#warning` does.
77-
<!-- SE-0397: or they can generate new declarations. -->
37+
独立宏可以像 `#function` 所做的那样产出一个值,也可以像 `#warning` 所做的那样在编译时执行一个操作。
38+
<!-- SE-0397: 或者他们也可以生成新的声明。 -->
7839

79-
## Attached Macros
40+
## 附加宏
8041

81-
To call an attached macro,
82-
you write an at sign (`@`) before its name,
83-
and you write any arguments to the macro in parentheses after its name.
42+
要调用附加宏,需要在其名称前写入 at 符号 (`@`) ,并在名称后的括号中写入宏的参数。
8443

85-
Attached macros modify the declaration that they're attached to.
86-
They add code to that declaration,
87-
like defining a new method or adding conformance to a protocol.
44+
附加宏会修改它们被所附加到的声明。它们为被附加到的声明添加代码,比如定义一个新的方法或者增加对某个协议的遵循。
8845

89-
For example, consider the following code
90-
that doesn't use macros:
46+
例如,请看下面这段不使用宏的代码:
9147

9248
```swift
9349
struct SundaeToppings: OptionSet {
@@ -98,14 +54,9 @@ struct SundaeToppings: OptionSet {
9854
}
9955
```
10056

101-
In this code,
102-
each of the options in the `SundaeToppings` option set
103-
includes a call to the initializer,
104-
which is repetitive and manual.
105-
It would be easy to make a mistake when adding a new option,
106-
like typing the wrong number at the end of the line.
57+
在这段代码中,`SundaeToppings` 选项集中的每个选项都包括对初始化器的调用,这是重复的手动操作。这样在添加新选项时容易出错,比如在行尾键入错误的数字。
10758

108-
Here's a version of this code that uses a macro instead:
59+
下面是该代码使用宏后的替代版本:
10960

11061
```swift
11162
@OptionSet<Int>
@@ -118,26 +69,19 @@ struct SundaeToppings {
11869
}
11970
```
12071

121-
This version of `SundaeToppings` calls an `@OptionSet` macro.
122-
The macro reads the list of cases in the private enumeration,
123-
generates the list of constants for each option,
124-
and adds a conformance to the [`OptionSet`][] protocol.
72+
此版本的 `SundaeToppings` 调用了 `@OptionSet` 宏。这个宏会读取私有枚举类中的 `case` 列表,生成每个选项的常量列表,并增加对 [`OptionSet`][] 协议的遵循。
12573

12674
[`OptionSet`]: https://developer.apple.com/documentation/swift/optionset
12775

12876
<!--
129-
When the @OptionSet macro comes back, change both links back:
77+
@OptionSet 宏回归后,把这俩链接改回来:
13078
13179
[`@OptionSet`]: https://developer.apple.com/documentation/swift/optionset-swift.macro
13280
[`OptionSet`]: https://developer.apple.com/documentation/swift/optionset-swift.protocol
13381
-->
13482

13583

136-
For comparison,
137-
here's what the expanded version of the `@OptionSet` macro looks like.
138-
You don't write this code,
139-
and you would see it only if you specifically asked Swift
140-
to show the macro's expansion.
84+
作为对比,下面是 `@OptionSet` 宏展开后的版本的样子。这段代码不是由你自己编写的,只有当你特别要求 Swift 显示宏的展开时,你才会看到它。
14185

14286
```swift
14387
struct SundaeToppings {
@@ -158,12 +102,7 @@ struct SundaeToppings {
158102
extension SundaeToppings: OptionSet { }
159103
```
160104

161-
All of the code after the private enumeration
162-
comes from the `@OptionSet` macro.
163-
The version of `SundaeToppings`
164-
that uses a macro to generate all of the static variables
165-
is easier to read and easier to maintain
166-
than the manually coded version, earlier.
105+
私有枚举类之后的所有代码都来自于 `@OptionSet` 宏。使用宏生成所有静态变量的 `SundaeToppings` 版本比前面手动编码的版本更易于阅读和维护。
167106

168107
## Macro Declarations
169108

0 commit comments

Comments
 (0)