-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathadvanced-operators.swift
103 lines (70 loc) · 2.44 KB
/
advanced-operators.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import Foundation
// # Bitwise Operators
var a: UInt8 = 0b00001111
var b: UInt8 = 0b11110000
// `NOT`
print(~a == b) // true
// `AND`
print(a & b == 0) // true
// `OR`
print(a | b == 0b11111111) // true
// `XOR`
print(a ^ b == a | b) // true
// Shifts (unsigned)
print(a << 1 == 0b00011110)
print(a >> 1 == 0b00000111)
// # Overflow Operators
// Normally Swift will error when and over/under
// flow occurs.
var num = UInt8.max
// error: arithmetic operation results in an overflow
/* var zero = num + 1 */
// But prefixing the operator with "&" tells Swift
// that you want this behavior.
var zero = num &+ 1
print(zero == 0) // true
// # Operator Functions
// Operator overloading is a supported language feature,
// but it isn't a best practice in most cases.
struct Trip {
var distance = 0.0, duration = 0.0
}
// Defines an 'infix' + operator that handles two args.
func + (left: Trip, right: Trip) -> Trip {
return Trip(distance: left.distance + right.distance,
duration: left.duration + right.duration)
}
// Defines a 'prefix' - operator that handles a single arg.
prefix func - (trip: Trip) -> Trip {
return Trip(distance: trip.distance * -1.0, duration: trip.duration * -1.0)
}
// Compound assignments are also supported.
func += (inout left: Trip, right: Trip) {
left = left + right
}
// As well as assignment expressions.
prefix func ++ (inout trip: Trip) -> Trip {
trip += Trip(distance: 1.0, duration: 1.0)
return trip
}
var tripA = Trip(distance: 100.0, duration: 2.0)
var tripB = Trip(distance: 250.0, duration: 5.0)
var combined = tripA + tripB
print("Went \(combined.distance) in \(combined.duration)") // Went 350.0 in 7.0
var disaster = -tripA
print("Went \(disaster.distance) in \(disaster.duration)") // Went -100.0 in -2.0
tripA += tripB
tripA += tripB
print("Went \(tripA.distance) in \(tripA.duration)") // Went 600.0 in 12.0
++tripA
print("Went \(tripA.distance) in \(tripA.duration)") // Went 601.0 in 13.0
// You can also define your own bananas custom operators with
// / = - + * % < > ! & | ^ . ~
postfix operator -=- {}
postfix func -=- (inout trip: Trip) -> Trip {
trip = Trip(distance: Double(Int(trip.distance) * random()),
duration: Double(Int(trip.duration) * random()))
return trip
}
tripA-=-
print("Went \(tripA.distance) in \(tripA.duration)") // Went 602222301.0 in 1311110111.0