1 00:00:06,630 --> 00:00:09,300 - Imagine you want to compare two objects for equality. 2 00:00:09,300 --> 00:00:10,140 Like I've done here, 3 00:00:10,140 --> 00:00:11,760 I've defined a point structure, 4 00:00:11,760 --> 00:00:13,680 an XY coordinate, 5 00:00:13,680 --> 00:00:15,690 I've created some point objects. 6 00:00:15,690 --> 00:00:16,620 What I'd like to do 7 00:00:16,620 --> 00:00:19,140 is to compare point objects for equality 8 00:00:19,140 --> 00:00:20,850 and for inequality. 9 00:00:20,850 --> 00:00:21,780 I'd like to do that 10 00:00:21,780 --> 00:00:24,090 but unfortunately this code won't compile 11 00:00:24,090 --> 00:00:25,530 because the point structure 12 00:00:25,530 --> 00:00:28,410 doesn't support equality and inequality. 13 00:00:28,410 --> 00:00:30,270 Rust doesn't know what it would mean 14 00:00:30,270 --> 00:00:31,680 to compare points for equality. 15 00:00:31,680 --> 00:00:33,090 How could it possibly know? 16 00:00:33,090 --> 00:00:34,710 You have to give it some help. 17 00:00:34,710 --> 00:00:38,280 The way to support inequality and equality checks in Rust 18 00:00:38,280 --> 00:00:40,533 is to implement a trait called PartialEq. 19 00:00:41,640 --> 00:00:46,110 There's a similar trait we'll discuss later on called Eq 20 00:00:46,110 --> 00:00:50,097 and I'll explain how the Eq trait differs from PartialEq. 21 00:00:50,940 --> 00:00:53,160 Okay, but to get the ball rolling 22 00:00:53,160 --> 00:00:56,310 we will just discuss the the most primitive of these two 23 00:00:56,310 --> 00:00:58,440 which is the PartialEq trait. 24 00:00:58,440 --> 00:00:59,823 It has two methods. 25 00:01:00,960 --> 00:01:02,790 It takes a parameter, 26 00:01:02,790 --> 00:01:04,200 a type parameter, 27 00:01:04,200 --> 00:01:05,940 representing the other kind of object 28 00:01:05,940 --> 00:01:07,680 you wanna compare yourself against. 29 00:01:07,680 --> 00:01:09,000 And by default 30 00:01:09,000 --> 00:01:11,670 it assumes that the right hand side type 31 00:01:11,670 --> 00:01:13,890 is the same as you, okay? 32 00:01:13,890 --> 00:01:14,723 So in other words 33 00:01:14,723 --> 00:01:17,190 if the left hand object is a point, 34 00:01:17,190 --> 00:01:19,767 it assumes that the right hand side object 35 00:01:19,767 --> 00:01:20,976 is also of that type point. 36 00:01:20,976 --> 00:01:24,150 You're comparing one point against another. 37 00:01:24,150 --> 00:01:25,920 We'll show an example of this later. 38 00:01:25,920 --> 00:01:27,750 There's one method you have to implement 39 00:01:27,750 --> 00:01:29,700 it's the Eq method. 40 00:01:29,700 --> 00:01:33,270 It takes you plus a reference to the other object 41 00:01:33,270 --> 00:01:34,103 which is presumed 42 00:01:34,103 --> 00:01:35,820 to be of the same type as you 43 00:01:35,820 --> 00:01:37,530 and it you have to implement this method 44 00:01:37,530 --> 00:01:38,363 to return a Boolean. 45 00:01:38,363 --> 00:01:39,930 In other words, you have to tell Rust 46 00:01:39,930 --> 00:01:43,620 what does it mean for two objects to be equal. 47 00:01:43,620 --> 00:01:46,530 There's also a not equal method NE, 48 00:01:46,530 --> 00:01:48,900 but that has a default implementation. 49 00:01:48,900 --> 00:01:50,010 So in practice 50 00:01:50,010 --> 00:01:53,220 you typically just implement the first method Eq. 51 00:01:53,220 --> 00:01:55,320 Rust will implement not equal 52 00:01:55,320 --> 00:01:58,740 automatically as an inverse of the equality check. 53 00:01:58,740 --> 00:02:00,330 So if it knows how to do equality 54 00:02:00,330 --> 00:02:02,913 it can infer how to do inequality. 55 00:02:04,020 --> 00:02:05,940 These functions are called automatically 56 00:02:05,940 --> 00:02:07,320 when you use the operators. 57 00:02:07,320 --> 00:02:09,990 So when you use the equals equals operator 58 00:02:09,990 --> 00:02:12,120 it'll automatically call the Eq function 59 00:02:12,120 --> 00:02:13,680 that you've implemented. 60 00:02:13,680 --> 00:02:15,990 And when you use the not equals operator 61 00:02:15,990 --> 00:02:18,210 it'll call the not equals function 62 00:02:18,210 --> 00:02:20,943 which has been presumably provided by Rust. 63 00:02:22,080 --> 00:02:25,173 Now the easiest way for you to support PartialEq 64 00:02:26,280 --> 00:02:28,180 for a structure is via hash derive 65 00:02:29,310 --> 00:02:30,840 and I've done that here. 66 00:02:30,840 --> 00:02:33,600 As long as each field in here 67 00:02:33,600 --> 00:02:36,090 supports the PartialEq type, 68 00:02:36,090 --> 00:02:38,760 i32 does support Partial equality 69 00:02:38,760 --> 00:02:39,750 and we know that 70 00:02:39,750 --> 00:02:42,780 because we can say I equals J. 71 00:02:42,780 --> 00:02:45,600 Integers know how to compare for equality. 72 00:02:45,600 --> 00:02:48,510 So as long as each field in your structure 73 00:02:48,510 --> 00:02:51,150 supports PartialEq type 74 00:02:51,150 --> 00:02:54,090 then you can just use hash derive, okay? 75 00:02:54,090 --> 00:02:57,030 On PartialEq or what the Rust compiler will do. 76 00:02:57,030 --> 00:02:59,550 When you compare two points for equality 77 00:02:59,550 --> 00:03:00,420 like I done here 78 00:03:00,420 --> 00:03:03,120 I've got some points being created. 79 00:03:03,120 --> 00:03:06,150 When I compare P1 equals P2 80 00:03:06,150 --> 00:03:09,450 it'll invoke the default version of the Eq method 81 00:03:09,450 --> 00:03:11,520 as generated by the compiler 82 00:03:11,520 --> 00:03:14,080 and it'll compare the X of P1 83 00:03:15,150 --> 00:03:18,122 for equality with the X of P2 84 00:03:18,122 --> 00:03:22,514 and it'll compare the Y of P1 and the Y of P2. 85 00:03:22,514 --> 00:03:25,590 So it kind of recursively calls the equality operations 86 00:03:25,590 --> 00:03:26,880 for each field 87 00:03:26,880 --> 00:03:28,620 to determine if the whole point 88 00:03:28,620 --> 00:03:29,850 is equal to the other point. 89 00:03:29,850 --> 00:03:31,735 If every field in here 90 00:03:31,735 --> 00:03:33,930 is equal to every field in here, 91 00:03:33,930 --> 00:03:35,190 then the equal, okay? 92 00:03:35,190 --> 00:03:36,810 And that's what happens here. 93 00:03:36,810 --> 00:03:41,100 So this syntax is implicitly calling the equality function. 94 00:03:41,100 --> 00:03:44,250 You can call it explicitly if you want to as well. 95 00:03:44,250 --> 00:03:45,990 Not much point in doing this to be frank, 96 00:03:45,990 --> 00:03:50,100 but you can you can pass in point 1.Eq 97 00:03:50,100 --> 00:03:52,410 I'm explicitly calling the equality function 98 00:03:52,410 --> 00:03:54,540 it'll pass P1 itself 99 00:03:54,540 --> 00:03:58,320 and it'll pass a reference to P2 as the other parameter. 100 00:03:58,320 --> 00:03:59,940 Here's the not equal comparison 101 00:03:59,940 --> 00:04:01,500 using the operator 102 00:04:01,500 --> 00:04:04,590 and here's the not equal comparison using the function. 103 00:04:04,590 --> 00:04:06,090 Obviously the point of these operators 104 00:04:06,090 --> 00:04:08,100 is that you use the operator syntax 105 00:04:08,100 --> 00:04:09,900 rather than using the function syntax 106 00:04:09,900 --> 00:04:11,253 unless you really want to. 107 00:04:12,630 --> 00:04:14,700 Okay, so that's good. 108 00:04:14,700 --> 00:04:16,080 Let's take an example of this. 109 00:04:16,080 --> 00:04:19,500 It's in the lesson 13 Generics project. 110 00:04:19,500 --> 00:04:22,500 We'll have a look at the main.rs at the start. 111 00:04:22,500 --> 00:04:24,960 I've got a an application file 112 00:04:24,960 --> 00:04:27,000 called Demo Partial equality 113 00:04:27,000 --> 00:04:29,460 using the derived mechanism, okay? 114 00:04:29,460 --> 00:04:31,050 So we'll run that first. 115 00:04:31,050 --> 00:04:31,950 Let's have a look. 116 00:04:32,820 --> 00:04:33,940 So in my main code 117 00:04:35,190 --> 00:04:37,710 I've got three as it turns out 118 00:04:37,710 --> 00:04:41,460 different demos of how to implement Partial equality. 119 00:04:41,460 --> 00:04:43,560 First of all, using the simplest approach 120 00:04:43,560 --> 00:04:46,110 using the derived mechanism, okay? 121 00:04:46,110 --> 00:04:48,540 So that's the first demo we'll have a look at 122 00:04:48,540 --> 00:04:49,383 and it's here. 123 00:04:50,850 --> 00:04:53,370 So I've got a point structure 124 00:04:53,370 --> 00:04:55,890 which doesn't implement Partial equality. 125 00:04:55,890 --> 00:05:00,003 I've got another point structure that does, okay, 126 00:05:01,419 --> 00:05:03,510 so here's a function, 127 00:05:03,510 --> 00:05:05,730 demo no Partial equality. 128 00:05:05,730 --> 00:05:07,500 In this example here, 129 00:05:07,500 --> 00:05:10,410 I've created instances of my point structure 130 00:05:10,410 --> 00:05:13,644 that doesn't support Partial equality okay? 131 00:05:13,644 --> 00:05:16,143 Point V1, doesn't support equality checks. 132 00:05:17,100 --> 00:05:21,330 So if I tried these statements here, they would fail 133 00:05:21,330 --> 00:05:24,897 because my point structure doesn't implement PartialEq. 134 00:05:26,010 --> 00:05:27,245 All right? 135 00:05:27,245 --> 00:05:28,078 So it would be disappointing. 136 00:05:28,950 --> 00:05:31,470 More fruitful would be the second demo, 137 00:05:31,470 --> 00:05:33,630 demo partial equality. 138 00:05:33,630 --> 00:05:37,050 And here I'm using P2, point V2 I should say. 139 00:05:37,050 --> 00:05:39,840 And that structure does support partially quality. 140 00:05:39,840 --> 00:05:41,790 So these will now work, 141 00:05:41,790 --> 00:05:43,320 I'll just prove the point 142 00:05:43,320 --> 00:05:44,673 that they do indeed work. 143 00:05:46,980 --> 00:05:48,243 cargo run. 144 00:05:49,350 --> 00:05:51,180 So I'm checking my points, 145 00:05:51,180 --> 00:05:54,780 I'm seeing does P1 equal P2? 146 00:05:54,780 --> 00:05:57,330 And it does in both syntaxes. 147 00:05:57,330 --> 00:06:00,000 Is P1 different from P3? 148 00:06:00,000 --> 00:06:01,170 Is it not equal? 149 00:06:01,170 --> 00:06:04,170 Is P1 not equal P3? 150 00:06:04,170 --> 00:06:05,671 And the answer is true. 151 00:06:05,671 --> 00:06:07,143 P1 and P3 are different. 152 00:06:08,790 --> 00:06:12,390 Okay then, so that is the simplest possible way 153 00:06:12,390 --> 00:06:14,460 to implement Partial equality 154 00:06:14,460 --> 00:06:17,040 using the derived attribute. 155 00:06:17,040 --> 00:06:19,080 What we're gonna do in the next demo 156 00:06:19,080 --> 00:06:21,630 is see how to implement Partial equality 157 00:06:21,630 --> 00:06:25,170 specifically where the algorithm is a bit more complicated 158 00:06:25,170 --> 00:06:27,510 than the compiler would be able to guess. 159 00:06:27,510 --> 00:06:29,700 So let's see how to do that. 160 00:06:29,700 --> 00:06:32,920 So you can provide a custom implementation of PartialEq 161 00:06:33,780 --> 00:06:35,880 useful where the equality check 162 00:06:35,880 --> 00:06:38,070 isn't just a matter of saying A equals A, 163 00:06:38,070 --> 00:06:39,543 B equals B 164 00:06:39,543 --> 00:06:41,057 but something more profound. 165 00:06:41,057 --> 00:06:43,050 So here's an example. 166 00:06:43,050 --> 00:06:46,740 I've got a class or structure I should say called angle. 167 00:06:46,740 --> 00:06:50,130 It represents an angle in a circle, you know? 168 00:06:50,130 --> 00:06:53,250 and what I want to do is to have the concept of wraparound. 169 00:06:53,250 --> 00:06:55,890 So every time it goes around 360 170 00:06:55,890 --> 00:06:57,600 it's basically the same angle. 171 00:06:57,600 --> 00:07:01,530 One degree is the same as 361 degrees. 172 00:07:01,530 --> 00:07:04,280 Two degrees is the same as 362 degrees 173 00:07:04,280 --> 00:07:07,173 in the sense that it land up pointing in the same place. 174 00:07:08,250 --> 00:07:10,380 Okay, so that's a bit of a contrived example, 175 00:07:10,380 --> 00:07:11,250 but here we go, 176 00:07:11,250 --> 00:07:13,140 I'm implementing PartialEq 177 00:07:13,140 --> 00:07:15,720 explicitly for the angle structure, 178 00:07:15,720 --> 00:07:19,320 the equality now, self will be an angle, 179 00:07:19,320 --> 00:07:21,300 other will be the same type 180 00:07:21,300 --> 00:07:24,330 and other will also be another angle. 181 00:07:24,330 --> 00:07:25,860 Okay so I've got two angles 182 00:07:25,860 --> 00:07:29,510 I take my degrees and modulus 360, 183 00:07:29,510 --> 00:07:32,100 in other words I basically divide by 360 184 00:07:32,100 --> 00:07:34,050 and just keep the remainder. 185 00:07:34,050 --> 00:07:37,160 So if it's 360 or 720 or whatever 1,080, 186 00:07:37,160 --> 00:07:38,910 is basically the same angle. 187 00:07:38,910 --> 00:07:41,430 So take the modulus of the first angle 188 00:07:41,430 --> 00:07:44,010 and take the modulus of the second angle. 189 00:07:44,010 --> 00:07:46,140 And if those numbers are left over as the same 190 00:07:46,140 --> 00:07:49,140 they are the same amount around the circle. 191 00:07:49,140 --> 00:07:51,660 So that's a non-trivial comparison. 192 00:07:51,660 --> 00:07:53,340 There's no way the compiler 193 00:07:53,340 --> 00:07:54,570 could possibly have guessed that 194 00:07:54,570 --> 00:07:57,153 because it doesn't understand angles like we do. 195 00:07:58,710 --> 00:07:59,543 There we are. 196 00:07:59,543 --> 00:08:01,980 So I'm gonna show you that example again. 197 00:08:01,980 --> 00:08:03,600 It's gonna be in same project, 198 00:08:03,600 --> 00:08:05,280 but this time PartialEq 199 00:08:05,280 --> 00:08:07,593 using an implemented approach explicitly. 200 00:08:08,670 --> 00:08:09,840 Okay so that's this one, 201 00:08:09,840 --> 00:08:13,620 Demo PartialEq implemented explicitly 202 00:08:13,620 --> 00:08:15,843 and it's here. 203 00:08:17,720 --> 00:08:21,510 Okay so here is my angle structure 204 00:08:21,510 --> 00:08:24,990 and here's my explicit implementation of PartialEq. 205 00:08:24,990 --> 00:08:26,670 And again, I'll just remind you 206 00:08:26,670 --> 00:08:30,150 that when you implement methods for a trait 207 00:08:30,150 --> 00:08:31,800 they are implicitly public. 208 00:08:31,800 --> 00:08:33,120 You don't need to say pub, 209 00:08:33,120 --> 00:08:33,960 in fact, you can't 210 00:08:33,960 --> 00:08:35,820 they are public anyway. 211 00:08:35,820 --> 00:08:37,203 I've got some angles here. 212 00:08:40,478 --> 00:08:42,750 90 is the same angle as 450, really. 213 00:08:42,750 --> 00:08:44,820 450 is once around the circle 214 00:08:44,820 --> 00:08:46,470 and then 90 degrees extra 215 00:08:46,470 --> 00:08:48,720 so it'll end up pointing to the same place. 216 00:08:48,720 --> 00:08:51,483 So in the kind of wraparound kind of way, 217 00:08:51,483 --> 00:08:52,684 A1 and A2 are equal 218 00:08:52,684 --> 00:08:55,770 and they will be tested as equal here. 219 00:08:55,770 --> 00:08:59,640 And angle one and angle three are different 220 00:08:59,640 --> 00:09:02,160 and they will prove to be different here. 221 00:09:02,160 --> 00:09:03,660 So let's run the example again 222 00:09:06,990 --> 00:09:09,600 and in my implemented partial equality 223 00:09:09,600 --> 00:09:11,670 angle one and angle two are true. 224 00:09:11,670 --> 00:09:15,180 Angle one and angle three are different, okay? 225 00:09:15,180 --> 00:09:16,680 So one reason 226 00:09:16,680 --> 00:09:20,700 for implementing the partial Partial equality 227 00:09:20,700 --> 00:09:22,950 structured trait type yourself 228 00:09:22,950 --> 00:09:26,070 is where the algorithm is something 229 00:09:26,070 --> 00:09:28,140 that the compiler couldn't guess, okay? 230 00:09:28,140 --> 00:09:29,550 And that's one reason 231 00:09:29,550 --> 00:09:33,540 for implementing Partial equality explicitly. 232 00:09:33,540 --> 00:09:34,890 There is another reason 233 00:09:34,890 --> 00:09:36,540 and that's where the type of object 234 00:09:36,540 --> 00:09:39,630 you're comparing against is a different type. 235 00:09:39,630 --> 00:09:41,701 And that's what we're going to look at 236 00:09:41,701 --> 00:09:43,200 in our third and final example. 237 00:09:43,200 --> 00:09:45,420 So it's possible for a structure 238 00:09:45,420 --> 00:09:46,680 to compare itself equal 239 00:09:46,680 --> 00:09:48,750 against a different structure type. 240 00:09:48,750 --> 00:09:50,340 Here's an example, 241 00:09:50,340 --> 00:09:52,200 I've got a Time Seconds 242 00:09:52,200 --> 00:09:54,570 it'll give you the time in seconds. 243 00:09:54,570 --> 00:09:58,130 It derives support for partial equality. 244 00:09:58,130 --> 00:09:59,700 In other words, you can say 245 00:09:59,700 --> 00:10:02,800 something like time in seconds one 246 00:10:04,470 --> 00:10:08,910 equals equals a time in seconds two. 247 00:10:08,910 --> 00:10:12,240 It understands comparing against time seconds. 248 00:10:12,240 --> 00:10:13,950 I've also got a time in minutes 249 00:10:13,950 --> 00:10:18,020 which also has derived support for Partial equality. 250 00:10:18,020 --> 00:10:22,110 In other words, if you had a time in minutes one 251 00:10:22,110 --> 00:10:24,000 then it would understand 252 00:10:24,000 --> 00:10:29,000 how to do a time in minutes two, right. 253 00:10:29,280 --> 00:10:34,280 But what if I wanted to do time second one 254 00:10:34,351 --> 00:10:36,690 equals time minute one? 255 00:10:36,690 --> 00:10:39,030 Oh double equals, excuse moi. 256 00:10:39,030 --> 00:10:41,430 What if I wanted to compare seconds and minutes? 257 00:10:41,430 --> 00:10:43,680 Well that's what this implementation does. 258 00:10:43,680 --> 00:10:47,760 It says for the time seconds structure, okay 259 00:10:47,760 --> 00:10:50,220 so that would be the self parameter. 260 00:10:50,220 --> 00:10:54,060 I can compare against time minutes. 261 00:10:54,060 --> 00:10:58,560 So I've implemented time minutes for time seconds. 262 00:10:58,560 --> 00:11:02,070 So it'll allow me to compare time seconds 263 00:11:02,070 --> 00:11:06,120 against time minutes like so and vice versa. 264 00:11:06,120 --> 00:11:08,760 Imagine I had a time in minutes 265 00:11:08,760 --> 00:11:12,450 and I wanted to compare against the time in seconds, okay? 266 00:11:12,450 --> 00:11:14,160 Because expressed in minutes and seconds 267 00:11:14,160 --> 00:11:15,990 they might be the same value 268 00:11:15,990 --> 00:11:17,700 but they're different types. 269 00:11:17,700 --> 00:11:20,220 So these derived structures 270 00:11:20,220 --> 00:11:23,670 they only work if the types are the same. 271 00:11:23,670 --> 00:11:24,783 What I've done here, 272 00:11:25,781 --> 00:11:28,110 is I've said for time minutes, 273 00:11:28,110 --> 00:11:29,520 I've implemented a comparison 274 00:11:29,520 --> 00:11:32,070 against apart with time seconds 275 00:11:32,070 --> 00:11:34,230 being the other type of parameter coming in. 276 00:11:34,230 --> 00:11:35,370 So the other type of parameter 277 00:11:35,370 --> 00:11:37,650 doesn't need to be the same as me, 278 00:11:37,650 --> 00:11:40,980 it can be a different type like so. 279 00:11:40,980 --> 00:11:43,200 Okay, so this doesn't happen very often obviously 280 00:11:43,200 --> 00:11:45,030 but it could happen 281 00:11:45,030 --> 00:11:47,040 and you can implement, 282 00:11:47,040 --> 00:11:49,180 basically specifying a type here 283 00:11:50,910 --> 00:11:53,160 which is different from your type 284 00:11:53,160 --> 00:11:55,200 and then you implement the logic. 285 00:11:55,200 --> 00:11:56,820 So I'm gonna show you the logic for that. 286 00:11:56,820 --> 00:11:59,880 Again, same project, we'll start off with main. 287 00:11:59,880 --> 00:12:01,592 The code we're going to run 288 00:12:01,592 --> 00:12:05,040 is in demo PartialEq implemented different types 289 00:12:05,040 --> 00:12:06,360 and then we'll run it. 290 00:12:06,360 --> 00:12:11,310 Okay so demo PartialEq implemented for different types 291 00:12:11,310 --> 00:12:13,170 and here we are. 292 00:12:13,170 --> 00:12:16,140 So I have my time in seconds, 293 00:12:16,140 --> 00:12:18,660 which will know how to compare itself 294 00:12:18,660 --> 00:12:20,340 against another time in seconds. 295 00:12:20,340 --> 00:12:22,170 I can say time in seconds one 296 00:12:22,170 --> 00:12:25,320 equals time in seconds two already. 297 00:12:25,320 --> 00:12:29,460 Likewise, I can compare two time in minutes for equality. 298 00:12:29,460 --> 00:12:33,300 It'll understand time minutes one equals time minutes two. 299 00:12:33,300 --> 00:12:35,040 And then I've implemented 300 00:12:35,040 --> 00:12:37,110 a combination of seconds and minutes 301 00:12:37,110 --> 00:12:38,220 and minutes and seconds. 302 00:12:38,220 --> 00:12:39,450 I've done it both way rounds. 303 00:12:39,450 --> 00:12:41,100 It's kind of associative, 304 00:12:41,100 --> 00:12:43,290 I said for time seconds, okay? 305 00:12:43,290 --> 00:12:45,870 So self will be time seconds. 306 00:12:45,870 --> 00:12:47,910 I can compare a time in seconds 307 00:12:47,910 --> 00:12:50,460 against a time in minutes. 308 00:12:50,460 --> 00:12:52,080 So a time in seconds 309 00:12:52,080 --> 00:12:54,120 equal a time in minutes, 310 00:12:54,120 --> 00:12:55,230 that would be true. 311 00:12:55,230 --> 00:12:57,000 A minute is 60 seconds. 312 00:12:57,000 --> 00:13:01,830 So if you multiply the other object minutes by seconds, 60 313 00:13:01,830 --> 00:13:05,970 then it'll check compare the actual seconds like so. 314 00:13:05,970 --> 00:13:07,920 And conversely, 315 00:13:07,920 --> 00:13:09,720 if I have a time in minutes 316 00:13:09,720 --> 00:13:12,300 so self is a time in minutes 317 00:13:12,300 --> 00:13:16,113 I can compare it against some other type in seconds. 318 00:13:17,100 --> 00:13:18,330 And you'll notice here 319 00:13:18,330 --> 00:13:21,090 that if you know how to compare seconds and minutes 320 00:13:21,090 --> 00:13:24,420 then minutes and seconds is just the other way round. 321 00:13:24,420 --> 00:13:27,813 So I've just leveraged the operation I've defined above. 322 00:13:28,800 --> 00:13:30,270 This function here 323 00:13:30,270 --> 00:13:33,933 allows me to compare seconds against minutes. 324 00:13:34,860 --> 00:13:39,860 Okay, so other here is seconds. 325 00:13:40,320 --> 00:13:42,960 So that is using seconds against minutes. 326 00:13:42,960 --> 00:13:45,600 This function here is calling that function there. 327 00:13:45,600 --> 00:13:47,970 I've avoided duplicating the code. 328 00:13:47,970 --> 00:13:49,920 I've just used the same function 329 00:13:49,920 --> 00:13:51,723 but swapped the parameters around. 330 00:13:52,710 --> 00:13:54,030 Okay, I was quite pleased with this 331 00:13:54,030 --> 00:13:55,380 when I thought about it. 332 00:13:55,380 --> 00:13:57,750 So I've got some times and seconds 333 00:13:57,750 --> 00:13:59,730 and I've got some times and minutes. 334 00:13:59,730 --> 00:14:02,550 I can use the derived implementation 335 00:14:02,550 --> 00:14:04,320 to compare seconds 336 00:14:04,320 --> 00:14:05,970 and inequality for seconds. 337 00:14:05,970 --> 00:14:08,700 So whenever I'm dealing with seconds everywhere 338 00:14:08,700 --> 00:14:13,140 then it'll be using the inbuilt seconds comparison. 339 00:14:13,140 --> 00:14:16,620 Likewise, it'll use the inbuilt minutes comparison 340 00:14:16,620 --> 00:14:19,140 if all I'm doing is comparing minutes with each other 341 00:14:19,140 --> 00:14:20,217 like I'm doing here 342 00:14:20,217 --> 00:14:22,380 and comparing minutes against minutes. 343 00:14:22,380 --> 00:14:23,970 So that's simple. 344 00:14:23,970 --> 00:14:27,510 And in this case I'm comparing seconds and minutes 345 00:14:27,510 --> 00:14:29,640 in doubt in various different syntaxes. 346 00:14:29,640 --> 00:14:31,683 So seconds 347 00:14:31,683 --> 00:14:34,350 is the structure type I'm working with 348 00:14:34,350 --> 00:14:36,060 and minutes is the other type 349 00:14:36,060 --> 00:14:40,200 so that will be provided by this one here. 350 00:14:40,200 --> 00:14:44,850 For my seconds where time seconds is self, 351 00:14:44,850 --> 00:14:45,930 the other type of object 352 00:14:45,930 --> 00:14:50,670 it'll be seconds equals equals minutes, okay? 353 00:14:50,670 --> 00:14:54,870 So those are supported here, 354 00:14:54,870 --> 00:14:58,020 seconds against minutes in any different syntax we like. 355 00:14:58,020 --> 00:14:59,880 And then the other way round 356 00:14:59,880 --> 00:15:01,830 minutes against seconds 357 00:15:01,830 --> 00:15:03,634 the object on the left 358 00:15:03,634 --> 00:15:05,060 always generates the self pointer, okay? 359 00:15:05,060 --> 00:15:07,350 So it's the minutes structure that we are looking at. 360 00:15:07,350 --> 00:15:10,800 Can I compare minutes against some other type seconds? 361 00:15:10,800 --> 00:15:12,270 Yes I can 362 00:15:12,270 --> 00:15:13,560 because for minutes 363 00:15:13,560 --> 00:15:17,220 I've implemented Partial equality with seconds, okay? 364 00:15:17,220 --> 00:15:21,750 So self will be minutes before the equals operator. 365 00:15:21,750 --> 00:15:24,240 The other parameter after the equals will be seconds. 366 00:15:24,240 --> 00:15:27,090 So I can say minutes equals equals seconds. 367 00:15:27,090 --> 00:15:30,750 So all we need to do is to run this 368 00:15:30,750 --> 00:15:33,453 and I really hope it works. 369 00:15:34,650 --> 00:15:36,630 I think it is going to work, isn't it? 370 00:15:36,630 --> 00:15:37,463 Yes. 371 00:15:37,463 --> 00:15:39,480 So we can compare seconds through equality 372 00:15:39,480 --> 00:15:41,373 using the derived implementation. 373 00:15:42,570 --> 00:15:45,199 Okay, so that's the derived implementation 374 00:15:45,199 --> 00:15:46,590 seconds against seconds here 375 00:15:48,270 --> 00:15:51,270 and then we can compare minutes against minutes, okay? 376 00:15:51,270 --> 00:15:53,250 And that's again using the derived implementation. 377 00:15:53,250 --> 00:15:54,780 And if that's as far as I needed to go 378 00:15:54,780 --> 00:15:56,490 then we could stop there. 379 00:15:56,490 --> 00:16:00,330 But I also want to support seconds and minutes. 380 00:16:00,330 --> 00:16:02,100 So for my time seconds, 381 00:16:02,100 --> 00:16:05,040 I support equality against minutes. 382 00:16:05,040 --> 00:16:07,530 Okay, so the other type coming in is minutes. 383 00:16:07,530 --> 00:16:11,040 So that implementation here covers this. 384 00:16:11,040 --> 00:16:12,690 And then finally, 385 00:16:12,690 --> 00:16:15,120 if I have minutes to compare against seconds, 386 00:16:15,120 --> 00:16:16,590 then for my minutes structure 387 00:16:16,590 --> 00:16:18,570 I can then compare against seconds. 388 00:16:18,570 --> 00:16:20,220 It's this implementation 389 00:16:20,220 --> 00:16:22,113 that supports these operations here. 390 00:16:23,040 --> 00:16:23,873 Hooray.