Making Your Own Operators

In addition to redefining operators, you can make your own. Custom operators can start with these characters:

/ = - + * % < > ! & | ^ . ~

They can also start with Unicode math, symbol, arrow, dingbat, and line/box drawing characters. All characters after the first one can be any of the preceding characters and/or Unicode characters. In this section, you’ll define the binary ~<*^*>~ operator, just to be extreme. (This arm-flailing-starry-eyed-looking-up operator won’t do anything impressive or useful.)

To define an operator, you must use the keyword operator and define it as prefix, infix, or postfix, like this:

infix operator ~<*^*>~ {}

Here you are putting the operator into existence. You haven’t actually defined what ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.