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

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.

主站蜘蛛池模板: 曲水县| 沧源| 襄汾县| 红桥区| 平顶山市| 资溪县| 灵寿县| 龙岩市| 谢通门县| 杭锦后旗| 无棣县| 从化市| 贵南县| 旅游| 池州市| 桐庐县| 平顶山市| 辉南县| 宣化县| 龙里县| 吉首市| 双城市| 探索| 灵宝市| 乳山市| 浪卡子县| 贵港市| 泾川县| 临湘市| 江永县| 福贡县| 策勒县| 大冶市| 边坝县| 凉城县| 清远市| 崇文区| 保德县| 保康县| 龙口市| 肇庆市|