Operator ExamplesUse custom operators, including equality, inequality, prefix, postfix and infix operator funcs.
This page was last reviewed on May 25, 2023.
Operators. In programs we use operators like "==" to compare things. But for some objects, we must define custom operators for equality.
And in rare situations, custom operators (not known to Swift) are helpful. This rapidly becomes complex. It can lead to code that is hard to read.
Equality example. Here we define equality and inequality operators. We specify a simple class called Box. On it we have a name String.
Info With the func keyword we create a global method that compares two Boxes (a left Box and aright Box).
Here We specify the "!=" operator and it returns the negation of the equality operator's result.
Tip The equality and inequality operators must return Bool. This indicates whether the objects are equal (or in equal).
Result We create 3 Box objects and compare them with our operators. The name field is used to determine equality and inequality.
class Box { var name: String? } func == (left: Box, right: Box) -> Bool { // Compare based on a String field. return left.name == right.name } func != (left: Box, right: Box) -> Bool { // Return opposite of equality. return !(left == right) } // Create three Box instances. var box1 = Box() box1.name = "blue" var box2 = Box() box2.name = "blue" var box3 = Box() box3.name = "red" // Compare the instances with equality operator. if box1 == box2 { print(true) } // Use inequality operator. if box1 != box3 { print(false) }
true false
Prefix, postfix, infix. Swift provides many language features for creating custom operators. This is an impractical example—it leads to unreadable code.
However The example uses the prefix, postfix, infix, associativity and precedence keywords.
Here We create instances of a simple Plane class. The Plane contains one field: an Int called "airborne."
Result We define three 3-character operators and use them on our plane1 and plane2 objects. The "airborne" property is modified.
// Declare some operators. prefix operator ??? {} postfix operator ^^^ {} infix operator *** { associativity left precedence 0 } // This class is used in the operator funcs. class Plane { var airborne = 1 } // Implement 3 custom operators. prefix func ??? (argument: Plane) { argument.airborne -= 10 } postfix func ^^^ (argument: Plane) { argument.airborne *= 20 } func *** (left: Plane, right: Plane) { left.airborne = right.airborne } // Create objects to use with operators. var plane1 = Plane() var plane2 = Plane() print("1 = \(plane1.airborne), \(plane2.airborne)") // Use prefix operator. ???plane1 print("2 = \(plane1.airborne), \(plane2.airborne)") // Use postfix operator. plane2^^^ print("3 = \(plane1.airborne), \(plane2.airborne)") // Use infix operator. plane1***plane2 print("4 = \(plane1.airborne), \(plane2.airborne)")
1 = 1, 1 2 = -9, 1 3 = -9, 20 4 = 20, 20
Some comments. The previous example is not a good example of code. Using 3-character operators would not be a good choice for code that is meant to be used.
However It does show that the Swift language is extensible. If a custom operator is needed, these keywords could be helpful.
Associativity, precedence. With an infix operator, we can define associativity and precedence. Some operators are grouped to the left or right—this is associativity.
And Some operators (like multiplication) have a higher precedence than others (like addition and subtraction).
Some languages have omitted operator overloading for clarity. Swift is not one of them. It has powerful, overloaded operators—this can lead to beautiful, concise, or unreadable code.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 25, 2023 (edit).
© 2007-2023 Sam Allen.