I hey Jack Davis here with themselves dotcom.
And I'd like to welcome you back for part for our journey into protocol oriented programming.
And the last lesson we covered the basics of protocol extensions and this lesson we're going to go.
We're going to briefly dive into generics.
Generics aren't strictly necessary for protocol oriented programming but they are used frequently and
I felt like this was a good place to introduce them.
Generics are another subject that can be pretty deep.
So this is just more of an introduction as with protocols and protocol extensions if you like what you
see.
I really encourage you to research these topics and try to grasp the concepts.
Learning these subjects can make you very desirable candidate to an employer.
So let's jump in and take a brief look at generics so we're going to set up a new playground here.
This on the screen you will just call this generics.
We'll leave it at this and put it on my desktop.
All right.
We'll get rid of this.
And so generics let's let's use a couple of examples here real quick and then I'll lay out the case
for generics.
So let's say that we wanted to create a function that took let's say took an integer and added 1 to
it.
So let's say funkin will say it after and we'll pass in a number it's a integer and returns an integer
and then we'll just return number plus 1.
Nice function.
Just call it an adder and Passey and say 15.
All right.
So we see here that this works.
We passed and 15 it returns 16.
All right.
Now what if we want to do the same thing with say doubles.
If we do it at her and past fifteen point zero we get an error because
fifteen point zero is a double it's not an integer.
So to fix that we can come back and we can say phunk double after passing a number and we'll say double
returned double and then we'll just turn number plus 1.
Whoops.
Now in the end if we say double
double adder and pass the fifteen point zero we get 16 again.
All right.
Now this is the problem that generics address.
This is a problem we're solving with them and basically wouldn't it be nice to have these two functions
here.
You just have a generic error function that you could pass any numeric type into and then it would turn
around and in no what you meant and return you a correct value based on that.
That's what generics do.
So let's just go ahead and dive in here we're going to create a generic adder and we're going past it.
Sorry we've got to specify this here not split second.
OK so here's our function and this might look a little weird to you.
This right here is just a placeholder.
Basically whatever type you pass the end of this right here is going to take the place of this T in
this declaration here.
This right here the method that you pass in or the value that you pass in.
I put this stipulation here that it has to conform to the Straubel protocol.
And then here's the actual number where we pass the end and it's going to return a value of the same
type that was passed in.
So if you pass in a double it's going to return double if you pass an integer.
It's going to return an integer.
And if you're familiar if you option click on this right here this gives you the declaration of stratabound
and conforming types are notionally continuous one dimensional values that can be offset and measured.
And that's kind of what gave me the clue that this right here would be the protocol that I was looking
for.
And you can you can confine these generics to different protocols are different types as we'll see some
more here in a minute.
But basically you can you can come and click on this and it'll take you to this standard library
and once we get in here we see this is the declaration for Strobl and I did it.
You had to do a lot of looking round sometimes in the standard library and documentation to be able
to find the right protocol or something to use for this.
But AUI Straubel was the right answer for this generic error.
So in order to call it all we've got to do is just say generic adder and see it gives us the placeholder
therefore the top.
So we're going past the end 15.
This is an integer.
So once we do that we say we get 6 10.
And if we pass in a double We still get 16.
So this is doing exactly what we're won.
We replace these two function with one generic function.
OK.
So that's the basics of how generics work and you don't always have to constrain the type to protocol.
But there's a lot of cases where you need to.
So let's let's look at another case here.
Let's look at a look at some functions we'll say in multiplier.
We're going to pass in two integers here.
And then we're going to return this like chess and or I just change for the left hand side right hand
side.
And you probably see that quite often in code.
All right so we've got an energy multiplier here.
It's going to take two integers and it's going to return the product to those two integers.
So in order to call that all we've got to do is just say multiplier impiously and say 2 and 5 and that
gives us 10.
So that's exactly what we're looking for.
But there again we can't use this for doubles.
So without generics we have to do
and we're going to return up some score.
There should be a double.
There should be a double and it's going to return a double
All right.
So all we've got to do now is just say we're turning left hand side times right hand side
and then we'll go ahead and call it we'll say Double no
double multiplier and we'll say two point zero times point zero and we get 10.
So how do we take these multiplier functions and do this generically.
Well it took me a little bit of research but I think I found an answer and it may not be the best thing
answer because you have to extend the types that you want to be able to use with it.
There's probably a different answer out there that may work out better.
This is just what I came up with in a short amount of time I had.
So first of all we're going to create a protocol
and we're just going to call the protocol new merch and we're going to add this function here for the
multiplication asterisk.
We're going to pass the left hand size and right hand side as well.
And we're going to return to self right.
Now that we've got that protocol we need to extend the types that we want to be able to use with this
protocol.
So lay out some extensions here and I'm just going to say double float and integer.
So we're going to extend that and add so that it conforms to that numeric protocol and all we've got
to do we don't have to put any implementation code in here.
All we've got to do is just X or conform to the numeric protocol in order to inherit or to gain this
function signature here.
And we'll add another extension for a float
metric and we'll do the same thing and it
all right.
Now that we've got that now we need to actually create the function.
So we'll do funk and we'll call it a generic multiplier
and we're going to do our generic Harim this time instead of Strobl.
We're going to constrain this to the numeric protocol that we just created and we're going to do the
same thing here left hand side takes it take the right hand side takes a T and we're going to return
t.
And this is just a placeholder.
You could put whatever you want to and then it's just a just like a label or just a token that it gets
replaced whenever the tops put in there.
All right so now we've got to do is turn left hand side Tom's right hand side and that should be all
that we've got to do to get this to work.
So let's do
generic multiplier.
And on the left hand side was passing in let's just start off with integer.
So we'll say five and five.
All right there we go get 25.
Do the same thing this time just pass in a flow say two point two times five and but that's correct.
So
yeah.
All right.
So that is that's just kind of a brief.
Now this last one was a little more complicated.
Basically I had to come up with my own protocol here and extend the taps and I wanted to use with it
with this function to conform to that protocol and the you know my constraint here for my generic constrained
it to that protocol.
So I hope that wasn't too complicated.
But there's a lot of power here and it gets pretty you guessed pretty day.
And I really suggest that you research this and find out all you can on generics if you're interested
in all that and I come in really handy it cuts down a lot of code.
And it's nice to be able to take a function instead of having to create 10 different functions for something
you can create one generic function and take care of all of it.
And generics are used in a lot of places so why that's our generic lesson hope it was helpful.
Hopefully one of these days we can put out some more advanced content on it and get a little deeper.
But that's what we've got for introduction to generics now.
So anyway thanks for join in and looks like that's the on introductory stuff.
The next lessons we get into will be some practical examples of ways you can use protocols are in the
problem or any problem.
So I hope you stick around.
So.