Skip to content

Commit 23a6b80

Browse files
committed
wip: Operator Declaration
1 parent b097d6a commit 23a6b80

File tree

1 file changed

+21
-49
lines changed

1 file changed

+21
-49
lines changed

swift-6-beta.docc/ReferenceManual/Declarations.md

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2658,84 +2658,56 @@ macro <#name#> = <#macro implementation#>
26582658
> *宏函数签名结果***`->`** *类型* \
26592659
> *宏定义***`=`** *表达式*
26602660
2661-
## Operator Declaration
2661+
## 操作符声明
26622662

2663-
An *operator declaration* introduces a new infix, prefix,
2664-
or postfix operator into your program
2665-
and is declared using the `operator` keyword.
2663+
*运算符声明*将新的中缀、前缀或后缀运算符引入到您的程序中,并使用 `operator` 关键字进行声明。
26662664

2667-
You can declare operators of three different fixities:
2668-
infix, prefix, and postfix.
2669-
The *fixity* of an operator specifies the relative position of an operator
2670-
to its operands.
2665+
您可以声明三种不同优先级的运算符:中缀、前缀和后缀。运算符的*优先级*指定了运算符相对于其操作数的相对位置。
26712666

2672-
There are three basic forms of an operator declaration,
2673-
one for each fixity.
2674-
The fixity of the operator is specified by marking the operator declaration with the
2675-
`infix`, `prefix`, or `postfix` declaration modifier before the `operator` keyword.
2676-
In each form, the name of the operator can contain only the operator characters
2677-
defined in <doc:LexicalStructure#Operators>.
2667+
运算符声明有三种基本形式,每种形式对应一种结合性(fixity)。运算符的结合性通过在 `operator` 关键字之前标注 `infix``prefix``postfix` 声明修饰符来指定。在每种形式中,运算符的名称只能包含 <doc:LexicalStructure#Operators> 中定义的运算符字符。
26782668

2679-
The following form declares a new infix operator:
2669+
以下形式声明了一个新的中缀运算符:
26802670

26812671
```swift
26822672
infix operator <#operator name#>: <#precedence group#>
26832673
```
26842674

2685-
An *infix operator* is a binary operator that's written between its two operands,
2686-
such as the familiar addition operator (`+`) in the expression `1 + 2`.
2675+
An *infix operator* is a binary operator that's written between its two operands, such as the familiar addition operator (`+`) in the expression `1 + 2`.
2676+
一个*中缀运算符*是一个二元运算符,它写在两个操作数之间,例如在表达式`1 + 2`中熟悉的加法运算符`+`
26872677

2688-
Infix operators can optionally specify a precedence group.
2689-
If you omit the precedence group for an operator,
2690-
Swift uses the default precedence group, `DefaultPrecedence`,
2691-
which specifies a precedence just higher than `TernaryPrecedence`.
2692-
For more information, see <doc:Declarations#Precedence-Group-Declaration>.
2678+
中缀运算符可以选择性地指定优先级组。如果您省略运算符的优先级组,Swift 将使用默认优先级组 `DefaultPrecedence`,该组的优先级仅高于 `TernaryPrecedence`。有关更多信息,请参见 <doc:Declarations#Precedence-Group-Declaration>
26932679

2694-
The following form declares a new prefix operator:
2680+
以下形式声明了一个新的前缀运算符:
26952681

26962682
```swift
26972683
prefix operator <#operator name#>
26982684
```
26992685

2700-
A *prefix operator* is a unary operator that's written immediately before its operand,
2701-
such as the prefix logical NOT operator (`!`) in the expression `!a`.
2686+
*前缀运算符* 是一种一元运算符,它直接写在操作数之前,例如表达式 `!a` 中的前缀逻辑非运算符(`!`)。
27022687

2703-
Prefix operators declarations don't specify a precedence level.
2704-
Prefix operators are nonassociative.
2688+
前缀运算符声明不指定优先级。前缀运算符是非结合的。
27052689

2706-
The following form declares a new postfix operator:
2690+
以下形式声明了一个新的后缀运算符:
27072691

27082692
```swift
27092693
postfix operator <#operator name#>
27102694
```
27112695

2712-
A *postfix operator* is a unary operator that's written immediately after its operand,
2713-
such as the postfix forced-unwrap operator (`!`) in the expression `a!`.
2696+
*后缀运算符*是一种一元运算符,它紧跟在操作数后面,例如在表达式 `a!` 中的后缀强制解包运算符 `!`
27142697

2715-
As with prefix operators, postfix operator declarations don't specify a precedence level.
2716-
Postfix operators are nonassociative.
2698+
与前缀运算符一样,后缀运算符声明不指定优先级。后缀运算符是非结合的。
27172699

2718-
After declaring a new operator,
2719-
you implement it by declaring a static method that has the same name as the operator.
2720-
The static method is a member of
2721-
one of the types whose values the operator takes as an argument ---
2722-
for example, an operator that multiplies a `Double` by an `Int`
2723-
is implemented as a static method on either the `Double` or `Int` structure.
2724-
If you're implementing a prefix or postfix operator,
2725-
you must also mark that method declaration with the corresponding `prefix` or `postfix`
2726-
declaration modifier.
2727-
To see an example of how to create and implement a new operator,
2728-
see <doc:AdvancedOperators#Custom-Operators>.
2700+
声明新运算符后,可以通过声明一个与运算符同名的静态方法来实现它。这个静态方法是运算符参数之一的类型的成员——例如,一个将 `Double``Int` 相乘的运算符可以在 `Double``Int` 结构上实现为静态方法。如果你要实现一个前缀或后缀运算符,还必须在方法声明中添加相应的 `prefix``postfix` 声明修饰符。要查看如何创建和实现新运算符的示例,请参阅 <doc:AdvancedOperators#Custom-Operators>
27292701

2730-
> Grammar of an operator declaration:
2702+
> 操作符声明的语法:
27312703
>
2732-
> *operator-declaration**prefix-operator-declaration* | *postfix-operator-declaration* | *infix-operator-declaration*
2704+
> *运算符声明**前缀运算符声明* | *后缀运算符声明* | *中缀运算符声明*
27332705
>
2734-
> *prefix-operator-declaration***`prefix`** **`operator`** *operator* \
2735-
> *postfix-operator-declaration***`postfix`** **`operator`** *operator* \
2736-
> *infix-operator-declaration***`infix`** **`operator`** *operator* *infix-operator-group*_?_
2706+
> *前缀运算符声明***`前缀`** **`运算符`** *运算符* \
2707+
> *后缀运算符声明***`后缀`** **`运算符`** *运算符* \
2708+
> *中缀运算符声明***`中缀`** **`运算符`** *运算符* *中缀运算符组*_?_
27372709
>
2738-
> *infix-operator-group***`:`** *precedence-group-name*
2710+
> *中缀运算符组***`:`** *优先级组名称*
27392711
27402712
## Precedence Group Declaration
27412713

0 commit comments

Comments
 (0)