官术网_书友最值得收藏!

Custom operator definition

We can define custom operators using the following syntax:

operatorType operator operatorName { } 

Here, operatorType can be one of the following:

  • prefix
  • infix
  • postfix

Custom infix operators can also specify a precedence and an associativity:

infix operator operatorName { associativity left/right/none precedence} 

The possible values for associativity are left, right, and none. Left-associative operators associate to the left if written next to other left-associative operators of the same precedence. Similarly, right-associative operators associate to the right if written next to other right-associative operators of the same precedence. Non-associative operators cannot be written next to other operators with the same precedence.

The associativity value defaults to none if it is not specified. The precedence value defaults to 100 if it is not specified.

Any custom operator defined with the preceding syntax will not have an existing meaning in Swift; therefore, a function with operatorName as its name should be defined and implemented. In the following section, we will examine an example of custom operator definition with its respective function definition.

主站蜘蛛池模板: 长乐市| 襄垣县| 璧山县| 香格里拉县| 临高县| 农安县| 安顺市| 连山| 红桥区| 萨迦县| 新田县| 北流市| 深圳市| 玉山县| 凌海市| 菏泽市| 灵璧县| 遂溪县| 固始县| 文成县| 乾安县| 襄汾县| 安泽县| 象州县| 石柱| 海口市| 公主岭市| 革吉县| 穆棱市| 松阳县| 句容市| 修水县| 平远县| 青岛市| 库尔勒市| 庆城县| 定远县| 九龙坡区| 通城县| 新民市| 孝昌县|