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

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.

主站蜘蛛池模板: 上林县| 诏安县| 夏河县| 乳山市| 美姑县| 延庆县| 通辽市| 崇州市| 靖边县| 乌审旗| 余姚市| 霍山县| 正阳县| 自治县| 石嘴山市| 芷江| 固安县| SHOW| 莎车县| 蓬溪县| 井冈山市| 郧西县| 宁晋县| 大竹县| 溧阳市| 出国| 沅陵县| 九台市| 柳江县| 房产| 平顶山市| 和林格尔县| 清河县| 萍乡市| 嘉善县| 金川县| 垣曲县| 亳州市| 卫辉市| 望城县| 萨嘎县|