1
00:00:03,440 --> 00:00:06,760
I hey Jack Davis here with themselves dotcom.

2
00:00:07,030 --> 00:00:13,650
And I'd like to welcome you back for part for our journey into protocol oriented programming.

3
00:00:13,830 --> 00:00:19,910
And the last lesson we covered the basics of protocol extensions and this lesson we're going to go.

4
00:00:20,070 --> 00:00:23,340
We're going to briefly dive into generics.

5
00:00:23,340 --> 00:00:29,110
Generics aren't strictly necessary for protocol oriented programming but they are used frequently and

6
00:00:29,120 --> 00:00:31,680
I felt like this was a good place to introduce them.

7
00:00:32,710 --> 00:00:35,520
Generics are another subject that can be pretty deep.

8
00:00:35,590 --> 00:00:41,800
So this is just more of an introduction as with protocols and protocol extensions if you like what you

9
00:00:41,800 --> 00:00:42,380
see.

10
00:00:42,400 --> 00:00:47,170
I really encourage you to research these topics and try to grasp the concepts.

11
00:00:48,160 --> 00:00:53,340
Learning these subjects can make you very desirable candidate to an employer.

12
00:00:53,380 --> 00:01:00,770
So let's jump in and take a brief look at generics so we're going to set up a new playground here.

13
00:01:01,050 --> 00:01:06,280
This on the screen you will just call this generics.

14
00:01:06,300 --> 00:01:12,580
We'll leave it at this and put it on my desktop.

15
00:01:12,790 --> 00:01:13,100
All right.

16
00:01:13,130 --> 00:01:15,350
We'll get rid of this.

17
00:01:15,730 --> 00:01:21,560
And so generics let's let's use a couple of examples here real quick and then I'll lay out the case

18
00:01:21,560 --> 00:01:23,170
for generics.

19
00:01:23,390 --> 00:01:30,380
So let's say that we wanted to create a function that took let's say took an integer and added 1 to

20
00:01:30,380 --> 00:01:31,380
it.

21
00:01:31,430 --> 00:01:41,240
So let's say funkin will say it after and we'll pass in a number it's a integer and returns an integer

22
00:01:42,740 --> 00:01:47,860
and then we'll just return number plus 1.

23
00:01:49,400 --> 00:01:51,020
Nice function.

24
00:01:51,290 --> 00:01:57,220
Just call it an adder and Passey and say 15.

25
00:01:57,610 --> 00:01:58,320
All right.

26
00:01:58,590 --> 00:02:00,210
So we see here that this works.

27
00:02:00,210 --> 00:02:02,890
We passed and 15 it returns 16.

28
00:02:03,220 --> 00:02:04,210
All right.

29
00:02:04,230 --> 00:02:08,460
Now what if we want to do the same thing with say doubles.

30
00:02:08,460 --> 00:02:18,630
If we do it at her and past fifteen point zero we get an error because

31
00:02:22,290 --> 00:02:25,560
fifteen point zero is a double it's not an integer.

32
00:02:26,370 --> 00:02:36,540
So to fix that we can come back and we can say phunk double after passing a number and we'll say double

33
00:02:37,050 --> 00:02:46,500
returned double and then we'll just turn number plus 1.

34
00:02:46,980 --> 00:02:49,560
Whoops.

35
00:02:49,680 --> 00:02:51,600
Now in the end if we say double

36
00:02:54,210 --> 00:03:00,790
double adder and pass the fifteen point zero we get 16 again.

37
00:03:05,060 --> 00:03:06,160
All right.

38
00:03:06,920 --> 00:03:10,160
Now this is the problem that generics address.

39
00:03:10,160 --> 00:03:15,500
This is a problem we're solving with them and basically wouldn't it be nice to have these two functions

40
00:03:15,710 --> 00:03:17,030
here.

41
00:03:17,430 --> 00:03:23,720
You just have a generic error function that you could pass any numeric type into and then it would turn

42
00:03:23,720 --> 00:03:29,670
around and in no what you meant and return you a correct value based on that.

43
00:03:29,750 --> 00:03:31,520
That's what generics do.

44
00:03:32,180 --> 00:03:40,020
So let's just go ahead and dive in here we're going to create a generic adder and we're going past it.

45
00:03:40,200 --> 00:03:44,310
Sorry we've got to specify this here not split second.

46
00:03:59,770 --> 00:04:05,050
OK so here's our function and this might look a little weird to you.

47
00:04:05,400 --> 00:04:07,920
This right here is just a placeholder.

48
00:04:08,490 --> 00:04:15,660
Basically whatever type you pass the end of this right here is going to take the place of this T in

49
00:04:15,660 --> 00:04:18,810
this declaration here.

50
00:04:19,230 --> 00:04:25,040
This right here the method that you pass in or the value that you pass in.

51
00:04:25,470 --> 00:04:31,870
I put this stipulation here that it has to conform to the Straubel protocol.

52
00:04:33,240 --> 00:04:39,180
And then here's the actual number where we pass the end and it's going to return a value of the same

53
00:04:39,180 --> 00:04:41,020
type that was passed in.

54
00:04:41,040 --> 00:04:44,840
So if you pass in a double it's going to return double if you pass an integer.

55
00:04:44,850 --> 00:04:46,360
It's going to return an integer.

56
00:04:46,890 --> 00:04:54,120
And if you're familiar if you option click on this right here this gives you the declaration of stratabound

57
00:04:55,380 --> 00:05:02,110
and conforming types are notionally continuous one dimensional values that can be offset and measured.

58
00:05:02,280 --> 00:05:06,210
And that's kind of what gave me the clue that this right here would be the protocol that I was looking

59
00:05:06,210 --> 00:05:07,040
for.

60
00:05:07,290 --> 00:05:14,730
And you can you can confine these generics to different protocols are different types as we'll see some

61
00:05:14,730 --> 00:05:16,930
more here in a minute.

62
00:05:17,160 --> 00:05:23,610
But basically you can you can come and click on this and it'll take you to this standard library

63
00:05:28,810 --> 00:05:37,470
and once we get in here we see this is the declaration for Strobl and I did it.

64
00:05:37,540 --> 00:05:42,790
You had to do a lot of looking round sometimes in the standard library and documentation to be able

65
00:05:42,790 --> 00:05:46,350
to find the right protocol or something to use for this.

66
00:05:47,110 --> 00:05:50,520
But AUI Straubel was the right answer for this generic error.

67
00:05:50,680 --> 00:05:59,560
So in order to call it all we've got to do is just say generic adder and see it gives us the placeholder

68
00:05:59,560 --> 00:06:00,830
therefore the top.

69
00:06:00,940 --> 00:06:02,470
So we're going past the end 15.

70
00:06:02,470 --> 00:06:05,850
This is an integer.

71
00:06:06,010 --> 00:06:09,130
So once we do that we say we get 6 10.

72
00:06:10,600 --> 00:06:16,280
And if we pass in a double We still get 16.

73
00:06:16,510 --> 00:06:19,150
So this is doing exactly what we're won.

74
00:06:19,210 --> 00:06:23,190
We replace these two function with one generic function.

75
00:06:24,180 --> 00:06:24,850
OK.

76
00:06:24,910 --> 00:06:34,190
So that's the basics of how generics work and you don't always have to constrain the type to protocol.

77
00:06:34,450 --> 00:06:37,300
But there's a lot of cases where you need to.

78
00:06:38,140 --> 00:06:41,130
So let's let's look at another case here.

79
00:06:41,140 --> 00:06:50,460
Let's look at a look at some functions we'll say in multiplier.

80
00:06:50,890 --> 00:06:55,810
We're going to pass in two integers here.

81
00:07:00,350 --> 00:07:08,270
And then we're going to return this like chess and or I just change for the left hand side right hand

82
00:07:08,270 --> 00:07:09,180
side.

83
00:07:09,650 --> 00:07:13,220
And you probably see that quite often in code.

84
00:07:14,320 --> 00:07:17,710
All right so we've got an energy multiplier here.

85
00:07:17,960 --> 00:07:22,270
It's going to take two integers and it's going to return the product to those two integers.

86
00:07:22,640 --> 00:07:35,030
So in order to call that all we've got to do is just say multiplier impiously and say 2 and 5 and that

87
00:07:35,030 --> 00:07:35,780
gives us 10.

88
00:07:35,780 --> 00:07:37,650
So that's exactly what we're looking for.

89
00:07:37,970 --> 00:07:40,310
But there again we can't use this for doubles.

90
00:07:40,400 --> 00:07:43,400
So without generics we have to do

91
00:07:54,580 --> 00:07:58,150
and we're going to return up some score.

92
00:07:58,260 --> 00:08:00,970
There should be a double.

93
00:08:01,620 --> 00:08:05,560
There should be a double and it's going to return a double

94
00:08:08,910 --> 00:08:09,960
All right.

95
00:08:10,290 --> 00:08:15,830
So all we've got to do now is just say we're turning left hand side times right hand side

96
00:08:19,680 --> 00:08:23,060
and then we'll go ahead and call it we'll say Double no

97
00:08:29,310 --> 00:08:39,580
double multiplier and we'll say two point zero times point zero and we get 10.

98
00:08:41,010 --> 00:08:47,210
So how do we take these multiplier functions and do this generically.

99
00:08:47,610 --> 00:08:52,290
Well it took me a little bit of research but I think I found an answer and it may not be the best thing

100
00:08:52,290 --> 00:08:56,690
answer because you have to extend the types that you want to be able to use with it.

101
00:08:56,700 --> 00:09:00,370
There's probably a different answer out there that may work out better.

102
00:09:00,450 --> 00:09:03,450
This is just what I came up with in a short amount of time I had.

103
00:09:03,720 --> 00:09:05,700
So first of all we're going to create a protocol

104
00:09:08,860 --> 00:09:14,260
and we're just going to call the protocol new merch and we're going to add this function here for the

105
00:09:14,260 --> 00:09:17,850
multiplication asterisk.

106
00:09:17,860 --> 00:09:23,450
We're going to pass the left hand size and right hand side as well.

107
00:09:23,800 --> 00:09:27,290
And we're going to return to self right.

108
00:09:28,480 --> 00:09:34,270
Now that we've got that protocol we need to extend the types that we want to be able to use with this

109
00:09:34,270 --> 00:09:35,710
protocol.

110
00:09:35,920 --> 00:09:43,050
So lay out some extensions here and I'm just going to say double float and integer.

111
00:09:43,600 --> 00:09:50,500
So we're going to extend that and add so that it conforms to that numeric protocol and all we've got

112
00:09:50,500 --> 00:09:53,570
to do we don't have to put any implementation code in here.

113
00:09:53,590 --> 00:10:02,830
All we've got to do is just X or conform to the numeric protocol in order to inherit or to gain this

114
00:10:02,830 --> 00:10:04,580
function signature here.

115
00:10:05,560 --> 00:10:09,140
And we'll add another extension for a float

116
00:10:12,130 --> 00:10:16,140
metric and we'll do the same thing and it

117
00:10:20,030 --> 00:10:20,980
all right.

118
00:10:21,290 --> 00:10:27,300
Now that we've got that now we need to actually create the function.

119
00:10:27,770 --> 00:10:32,390
So we'll do funk and we'll call it a generic multiplier

120
00:10:34,790 --> 00:10:39,620
and we're going to do our generic Harim this time instead of Strobl.

121
00:10:39,680 --> 00:10:48,290
We're going to constrain this to the numeric protocol that we just created and we're going to do the

122
00:10:48,280 --> 00:10:56,120
same thing here left hand side takes it take the right hand side takes a T and we're going to return

123
00:10:56,310 --> 00:10:57,600
t.

124
00:10:58,130 --> 00:11:00,260
And this is just a placeholder.

125
00:11:00,440 --> 00:11:08,990
You could put whatever you want to and then it's just a just like a label or just a token that it gets

126
00:11:08,990 --> 00:11:12,330
replaced whenever the tops put in there.

127
00:11:13,340 --> 00:11:22,550
All right so now we've got to do is turn left hand side Tom's right hand side and that should be all

128
00:11:22,550 --> 00:11:25,680
that we've got to do to get this to work.

129
00:11:26,060 --> 00:11:27,560
So let's do

130
00:11:31,360 --> 00:11:32,890
generic multiplier.

131
00:11:33,290 --> 00:11:38,370
And on the left hand side was passing in let's just start off with integer.

132
00:11:38,690 --> 00:11:40,700
So we'll say five and five.

133
00:11:40,700 --> 00:11:45,210
All right there we go get 25.

134
00:11:45,860 --> 00:11:54,750
Do the same thing this time just pass in a flow say two point two times five and but that's correct.

135
00:11:54,980 --> 00:11:55,580
So

136
00:12:00,060 --> 00:12:00,970
yeah.

137
00:12:01,150 --> 00:12:01,680
All right.

138
00:12:01,720 --> 00:12:06,050
So that is that's just kind of a brief.

139
00:12:06,160 --> 00:12:08,600
Now this last one was a little more complicated.

140
00:12:08,890 --> 00:12:14,710
Basically I had to come up with my own protocol here and extend the taps and I wanted to use with it

141
00:12:15,070 --> 00:12:22,630
with this function to conform to that protocol and the you know my constraint here for my generic constrained

142
00:12:22,630 --> 00:12:24,440
it to that protocol.

143
00:12:24,940 --> 00:12:27,610
So I hope that wasn't too complicated.

144
00:12:28,240 --> 00:12:32,560
But there's a lot of power here and it gets pretty you guessed pretty day.

145
00:12:33,340 --> 00:12:40,210
And I really suggest that you research this and find out all you can on generics if you're interested

146
00:12:40,210 --> 00:12:44,450
in all that and I come in really handy it cuts down a lot of code.

147
00:12:44,760 --> 00:12:50,500
And it's nice to be able to take a function instead of having to create 10 different functions for something

148
00:12:50,500 --> 00:12:53,950
you can create one generic function and take care of all of it.

149
00:12:54,430 --> 00:13:03,290
And generics are used in a lot of places so why that's our generic lesson hope it was helpful.

150
00:13:03,310 --> 00:13:07,830
Hopefully one of these days we can put out some more advanced content on it and get a little deeper.

151
00:13:08,110 --> 00:13:11,320
But that's what we've got for introduction to generics now.

152
00:13:11,320 --> 00:13:18,250
So anyway thanks for join in and looks like that's the on introductory stuff.

153
00:13:18,280 --> 00:13:23,350
The next lessons we get into will be some practical examples of ways you can use protocols are in the

154
00:13:23,350 --> 00:13:25,580
problem or any problem.

155
00:13:25,780 --> 00:13:27,290
So I hope you stick around.

156
00:13:27,370 --> 00:13:27,730
So.
