1
00:00:04,150 --> 00:00:10,790
Hey Dave is here with Fobes dot com and I'd like to welcome you back part three of our journey into

2
00:00:10,800 --> 00:00:12,720
procol programming.

3
00:00:13,340 --> 00:00:18,900
And the last lesson we've covered the basics of protocols and this list and we're going to dive into

4
00:00:18,900 --> 00:00:20,480
protocol extensions.

5
00:00:20,880 --> 00:00:25,990
And as I said previously protocol extensions are where the magic happens.

6
00:00:26,160 --> 00:00:29,660
You can extend any protocol to give them functionality.

7
00:00:29,730 --> 00:00:35,130
This means that you can not only exchange your own protocols but any protocol from a standard library

8
00:00:35,130 --> 00:00:42,310
is well protocol extensions were introduced as a way around some of the limitations of working with

9
00:00:42,310 --> 00:00:45,700
value types which offer no inheritance.

10
00:00:45,700 --> 00:00:51,340
Now you can place implementation code in an extension and it's available to any class or stroke that

11
00:00:51,340 --> 00:00:52,970
conforms to that protocol.

12
00:00:54,230 --> 00:01:00,890
So without any further ado let's go ahead and get started and we'll dive back in where we left off in

13
00:01:00,890 --> 00:01:02,900
the last listen.

14
00:01:02,940 --> 00:01:08,270
So I've got my playground open here that we had open in the last lesson that we were playing with.

15
00:01:08,490 --> 00:01:16,830
If you remember we did a vehicle protocol and then we create a sports car strapped and a semi truck

16
00:01:16,830 --> 00:01:25,250
strapped and we create instances of those and then we start on blue the air horn turned them off.

17
00:01:25,250 --> 00:01:26,830
So for.

18
00:01:27,010 --> 00:01:30,940
So we're going to extend or we're going use procol extensions today.

19
00:01:31,800 --> 00:01:36,290
To try to explain them to you and let you see the power that I have.

20
00:01:37,270 --> 00:01:45,250
Now this is just a kind of introductory lesson in protocols and protocols protocol extensions so they

21
00:01:45,250 --> 00:01:47,190
get really complicated.

22
00:01:47,770 --> 00:01:51,290
But there's a lot of power there and they're really nice.

23
00:01:51,330 --> 00:01:58,880
So we're just going to kind of go over the basics here so you kind of get an idea of what's going on.

24
00:01:59,960 --> 00:02:09,620
So in our vehicle protocol here we've got our is running low in property and we've got these two mutating

25
00:02:09,620 --> 00:02:10,700
Funk's here.

26
00:02:11,060 --> 00:02:15,680
I want to go ahead and add and make

27
00:02:24,810 --> 00:02:35,440
when I up making a model for our vehicles.

28
00:02:36,380 --> 00:02:45,110
And then we're going to go ahead and create our extension right here and we'll just do extension vehicle

29
00:02:46,130 --> 00:02:47,570
and that's really all there is to it.

30
00:02:47,570 --> 00:02:51,110
As far as declaring the extension

31
00:02:54,570 --> 00:02:56,730
of course we're getting these errors here.

32
00:02:58,950 --> 00:03:01,670
So this is mostly the model.

33
00:03:01,680 --> 00:03:03,170
There we go.

34
00:03:03,430 --> 00:03:09,280
We're getting errors here that this doesn't conform any more because we've added these two properties

35
00:03:09,280 --> 00:03:09,780
here.

36
00:03:10,740 --> 00:03:17,020
So let's run down and add those into our sports core

37
00:03:26,020 --> 00:03:27,640
sports car Astra.

38
00:03:27,840 --> 00:03:34,520
So where did we go down here to our semi truck it's a class.

39
00:03:34,620 --> 00:03:44,390
Watch what happens when I add these two properties.

40
00:03:44,710 --> 00:03:49,630
An error now an error says that there is no initializers.

41
00:03:50,260 --> 00:03:55,960
And what's going on here in this is running we gave it a default value of false.

42
00:03:55,960 --> 00:04:00,980
So when there was no initialiser it didn't complain because it had a value on these two.

43
00:04:00,990 --> 00:04:04,440
We don't have a value so we're going to have to provide an initialiser.

44
00:04:05,080 --> 00:04:06,880
So we'll do it

45
00:04:21,450 --> 00:04:25,140
and then we'll just set the set of properties up here.

46
00:04:38,610 --> 00:04:44,640
Mo okay and that should have taken care of here.

47
00:04:45,720 --> 00:04:50,770
Except now we're getting them down here because we have it we didn't declare these correctly.

48
00:04:51,010 --> 00:04:53,670
Avoid comment that out for just a minute.

49
00:04:53,670 --> 00:04:56,190
Actually we're going come in all this out for me.

50
00:04:58,160 --> 00:04:58,860
OK.

51
00:04:58,930 --> 00:05:04,130
So let's go back to our extension in here.

52
00:05:05,140 --> 00:05:08,780
I'm going to go ahead and change the way that we laid this out.

53
00:05:09,780 --> 00:05:17,850
So if you say we've got duplicated code then we've got a set of start and turn off method in both of

54
00:05:17,850 --> 00:05:19,770
these types here.

55
00:05:21,290 --> 00:05:30,320
So what I'm going to do is we're going to say mutating from store to

56
00:05:33,260 --> 00:05:37,760
in mutating.

57
00:05:38,030 --> 00:05:42,110
Turn off.

58
00:05:42,630 --> 00:05:43,400
All right.

59
00:05:43,590 --> 00:05:48,160
Now the end in these methods here we're going to check.

60
00:05:48,200 --> 00:05:56,220
So for the start we're going to say if is running just like we did before and if it is we will say present

61
00:05:56,490 --> 00:05:57,680
already started

62
00:06:02,530 --> 00:06:11,590
otherwise we're going to say is running Google's true and we're going to print.

63
00:06:12,160 --> 00:06:16,390
We're going to print the descriptions so

64
00:06:23,830 --> 00:06:27,400
so self description and then fired up.

65
00:06:27,420 --> 00:06:37,130
If you'll remember we added this custom string convertible here which gave us that description property.

66
00:06:37,160 --> 00:06:42,470
All right so now let's go ahead and implement our turn off function.

67
00:06:42,500 --> 00:06:49,340
It's all safe is running do is running is false.

68
00:06:49,390 --> 00:07:00,460
Print it and we'll do the same thing here we'll do self description and shit.

69
00:07:01,670 --> 00:07:06,620
And the reason we're doing this is because we're implementing this in this extension and bought the

70
00:07:06,620 --> 00:07:12,790
sports car and a semi truck are going to get these methods because we're implementing it in the extension.

71
00:07:12,890 --> 00:07:23,730
So basically we need to pull these out of here and just delete those.

72
00:07:24,920 --> 00:07:29,970
And again instructs require mutating functions classes don't.

73
00:07:30,230 --> 00:07:33,750
So we added the mutating key word here.

74
00:07:34,060 --> 00:07:39,650
But even though we've it's available in a in a class it it doesn't matter it just ignores that mutating

75
00:07:39,690 --> 00:07:40,730
keyword.

76
00:07:41,310 --> 00:07:42,740
All right.

77
00:07:44,770 --> 00:07:46,290
Where are we at here.

78
00:07:48,140 --> 00:07:49,960
Are backing our extinction.

79
00:07:53,120 --> 00:07:56,420
Say I put the else statement in here to know

80
00:08:14,600 --> 00:08:15,660
OK.

81
00:08:16,170 --> 00:08:20,820
So now we've got our turn off and start functions here and they're implemented.

82
00:08:20,820 --> 00:08:26,730
So sports car and semi truck are going to get those automatically because we conform to the vehicle

83
00:08:26,730 --> 00:08:28,050
protocol.

84
00:08:28,050 --> 00:08:32,850
Now I'm going to go in here and change up the description just a little bit and we're just going to

85
00:08:34,470 --> 00:08:36,200
return self

86
00:08:39,360 --> 00:08:41,640
self dot.

87
00:08:41,810 --> 00:08:43,490
See you on one second.

88
00:08:48,930 --> 00:08:51,770
Let you know actually let's do something different here.

89
00:08:52,840 --> 00:08:56,650
It was come back up here and let's say it's something else in our expansion.

90
00:08:56,730 --> 00:08:59,670
So let's add another property and we'll just call this make model

91
00:09:02,580 --> 00:09:04,520
and we'll just return

92
00:09:15,060 --> 00:09:21,330
we'll just return and make in a model that way whatever we get we'll just pass that in the description

93
00:09:21,330 --> 00:09:22,980
here.

94
00:09:25,890 --> 00:09:35,130
So we'll just for the description which is return self make model and then we'll do the same thing down

95
00:09:35,130 --> 00:09:47,250
here in the in the big league we'll just say return self to make model.

96
00:09:47,560 --> 00:09:48,120
All right.

97
00:09:50,450 --> 00:10:00,250
Now let's come back down here to where we declare these let's call it an and this and you'll see.

98
00:10:00,260 --> 00:10:07,130
Now I've got an auto complete suggestion what we need put there.

99
00:10:07,190 --> 00:10:13,720
So we've got is running and by default we're going to say false and make let's say this is this is a

100
00:10:13,720 --> 00:10:24,100
sports car so Porsche models say 9 11.

101
00:10:25,800 --> 00:10:30,780
The name will come semi truck here.

102
00:10:31,040 --> 00:10:33,970
We're getting the same thing not running.

103
00:10:34,340 --> 00:10:40,850
This is a Peter real and it's virago.

104
00:10:41,000 --> 00:10:44,360
I have no idea if that's a real truck or not but we're going to go with it.

105
00:10:45,610 --> 00:10:46,260
All right.

106
00:10:46,290 --> 00:10:48,960
In the end at that point let's go ahead and comment.

107
00:10:48,950 --> 00:11:00,580
These And so you see now are start methods are retired in the make and model and fired up make and model

108
00:11:00,580 --> 00:11:07,260
fired up and we still blow in the air horn because it's implemented just in a semi truck class

109
00:11:11,810 --> 00:11:13,120
and will turn off.

110
00:11:13,110 --> 00:11:15,420
And now the Peterbilt RAGO shut down

111
00:11:19,460 --> 00:11:28,650
and for here let's pretend we're cool dot make model.

112
00:11:28,680 --> 00:11:29,280
All right.

113
00:11:29,340 --> 00:11:36,060
So we can see here they are right now is putting out a Porsche 911 and appear before Argo.

114
00:11:36,390 --> 00:11:40,060
So that's kind of the power of that protocol extensions bring you.

115
00:11:40,350 --> 00:11:50,570
You can actually implement functions and computed properties in your extensions and then anything that

116
00:11:50,950 --> 00:11:58,530
that conforms to those two that protocol will automatically get that functionality.

117
00:11:58,530 --> 00:12:03,150
This is just kind of a brief overview of it but I think you could probably see the power

118
00:12:07,110 --> 00:12:14,140
so it's real similar to an extension of a type which if you'll recall

119
00:12:18,270 --> 00:12:24,480
let's just extend it double.

120
00:12:25,800 --> 00:12:30,200
So we're going to extend it and we're going to give it a dollar string.

121
00:12:31,020 --> 00:12:32,410
We're going to retire return

122
00:12:35,450 --> 00:12:45,470
string format and we're going to say a dollar sign and then we want to pad with zeros to places and

123
00:12:45,470 --> 00:12:47,510
we want to sell.

124
00:12:48,080 --> 00:12:49,550
So we've just extended a double here.

125
00:12:49,550 --> 00:12:55,700
Now this is a protocol extension this is a this is a top extension which you can do the same sort of

126
00:12:55,700 --> 00:13:02,510
thing any protocol that's in the standard library or any protocol that that you implement yourself.

127
00:13:02,510 --> 00:13:04,640
You can extend it in this way.

128
00:13:04,640 --> 00:13:12,620
So in this case right here we could say our per seat equals let's just say thirty two point one five

129
00:13:13,430 --> 00:13:22,680
times zero point one five Ok so we got four point eight two to five and we want to express that as a

130
00:13:22,680 --> 00:13:23,710
dollar.

131
00:13:24,210 --> 00:13:29,440
So all we've got to do now that we have that extension is percent dollar.

132
00:13:31,510 --> 00:13:32,290
And now you see

133
00:13:35,940 --> 00:13:45,390
I left out the Persius or so now you can see that just by adding this little extension here.

134
00:13:45,640 --> 00:13:51,460
Any double top now that we use from this point forward we'll have this functionality and it works the

135
00:13:51,460 --> 00:13:53,550
same way with it with protocols.

136
00:13:53,830 --> 00:14:02,320
So any extension that you add all of your types that conform to that protocol get that functionality

137
00:14:02,770 --> 00:14:04,720
just lock in this type extension.

138
00:14:04,870 --> 00:14:11,020
All doubles from now on to my program or my application will have the ability to call dollar string

139
00:14:11,080 --> 00:14:15,300
and print out a nicely formatted dollar amount.

140
00:14:15,400 --> 00:14:24,130
So I know this is a fairly short lesson here but I think it pretty well demonstrated the power of protocols

141
00:14:24,340 --> 00:14:31,010
protocol extensions and in the next lesson we're going to go for generics briefly kind of like this

142
00:14:31,630 --> 00:14:36,040
and then after that we'll get into some real world practical

143
00:14:39,680 --> 00:14:43,170
uses for protocols and protocols in a program.

144
00:14:43,170 --> 00:14:45,410
So until next time we'll see.
