@@ -2658,84 +2658,56 @@ macro <#name#> = <#macro implementation#>
2658
2658
> * 宏函数签名结果* → ** ` -> ` ** * 类型* \
2659
2659
> * 宏定义* → ** ` = ` ** * 表达式*
2660
2660
2661
- ## Operator Declaration
2661
+ ## 操作符声明
2662
2662
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 ` 关键字进行声明。
2666
2664
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
+ 您可以声明三种不同优先级的运算符:中缀、前缀和后缀。运算符的* 优先级* 指定了运算符相对于其操作数的相对位置。
2671
2666
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 > 中定义的运算符字符。
2678
2668
2679
- The following form declares a new infix operator:
2669
+ 以下形式声明了一个新的中缀运算符:
2680
2670
2681
2671
``` swift
2682
2672
infix operator < #operator name#>: <#precedence group#>
2683
2673
```
2684
2674
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 ` 中熟悉的加法运算符 ` + ` 。
2687
2677
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 > 。
2693
2679
2694
- The following form declares a new prefix operator:
2680
+ 以下形式声明了一个新的前缀运算符:
2695
2681
2696
2682
``` swift
2697
2683
prefix operator < #operator name#>
2698
2684
```
2699
2685
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 ` 中的前缀逻辑非运算符(` ! ` )。
2702
2687
2703
- Prefix operators declarations don't specify a precedence level.
2704
- Prefix operators are nonassociative.
2688
+ 前缀运算符声明不指定优先级。前缀运算符是非结合的。
2705
2689
2706
- The following form declares a new postfix operator:
2690
+ 以下形式声明了一个新的后缀运算符:
2707
2691
2708
2692
``` swift
2709
2693
postfix operator < #operator name#>
2710
2694
```
2711
2695
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! ` 中的后缀强制解包运算符 ` ! ` 。
2714
2697
2715
- As with prefix operators, postfix operator declarations don't specify a precedence level.
2716
- Postfix operators are nonassociative.
2698
+ 与前缀运算符一样,后缀运算符声明不指定优先级。后缀运算符是非结合的。
2717
2699
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 > 。
2729
2701
2730
- > Grammar of an operator declaration:
2702
+ > 操作符声明的语法:
2731
2703
>
2732
- > * operator-declaration * → * prefix-operator-declaration * | * postfix-operator-declaration * | * infix-operator-declaration *
2704
+ > * 运算符声明 * → * 前缀运算符声明 * | * 后缀运算符声明 * | * 中缀运算符声明 *
2733
2705
>
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
+ > * 中缀运算符声明 * → ** ` 中缀 ` ** ** ` 运算符 ` ** * 运算符 * * 中缀运算符组 * _ ?_
2737
2709
>
2738
- > * infix-operator-group * → ** ` : ` ** * precedence-group-name *
2710
+ > * 中缀运算符组 * → ** ` : ` ** * 优先级组名称 *
2739
2711
2740
2712
## Precedence Group Declaration
2741
2713
0 commit comments