Hey Dave is here with Fobes dot com and I'd like to welcome you back part three of our journey into
procol programming.
And the last lesson we've covered the basics of protocols and this list and we're going to dive into
protocol extensions.
And as I said previously protocol extensions are where the magic happens.
You can extend any protocol to give them functionality.
This means that you can not only exchange your own protocols but any protocol from a standard library
is well protocol extensions were introduced as a way around some of the limitations of working with
value types which offer no inheritance.
Now you can place implementation code in an extension and it's available to any class or stroke that
conforms to that protocol.
So without any further ado let's go ahead and get started and we'll dive back in where we left off in
the last listen.
So I've got my playground open here that we had open in the last lesson that we were playing with.
If you remember we did a vehicle protocol and then we create a sports car strapped and a semi truck
strapped and we create instances of those and then we start on blue the air horn turned them off.
So for.
So we're going to extend or we're going use procol extensions today.
To try to explain them to you and let you see the power that I have.
Now this is just a kind of introductory lesson in protocols and protocols protocol extensions so they
get really complicated.
But there's a lot of power there and they're really nice.
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.
So in our vehicle protocol here we've got our is running low in property and we've got these two mutating
Funk's here.
I want to go ahead and add and make
when I up making a model for our vehicles.
And then we're going to go ahead and create our extension right here and we'll just do extension vehicle
and that's really all there is to it.
As far as declaring the extension
of course we're getting these errors here.
So this is mostly the model.
There we go.
We're getting errors here that this doesn't conform any more because we've added these two properties
here.
So let's run down and add those into our sports core
sports car Astra.
So where did we go down here to our semi truck it's a class.
Watch what happens when I add these two properties.
An error now an error says that there is no initializers.
And what's going on here in this is running we gave it a default value of false.
So when there was no initialiser it didn't complain because it had a value on these two.
We don't have a value so we're going to have to provide an initialiser.
So we'll do it
and then we'll just set the set of properties up here.
Mo okay and that should have taken care of here.
Except now we're getting them down here because we have it we didn't declare these correctly.
Avoid comment that out for just a minute.
Actually we're going come in all this out for me.
OK.
So let's go back to our extension in here.
I'm going to go ahead and change the way that we laid this out.
So if you say we've got duplicated code then we've got a set of start and turn off method in both of
these types here.
So what I'm going to do is we're going to say mutating from store to
in mutating.
Turn off.
All right.
Now the end in these methods here we're going to check.
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
already started
otherwise we're going to say is running Google's true and we're going to print.
We're going to print the descriptions so
so self description and then fired up.
If you'll remember we added this custom string convertible here which gave us that description property.
All right so now let's go ahead and implement our turn off function.
It's all safe is running do is running is false.
Print it and we'll do the same thing here we'll do self description and shit.
And the reason we're doing this is because we're implementing this in this extension and bought the
sports car and a semi truck are going to get these methods because we're implementing it in the extension.
So basically we need to pull these out of here and just delete those.
And again instructs require mutating functions classes don't.
So we added the mutating key word here.
But even though we've it's available in a in a class it it doesn't matter it just ignores that mutating
keyword.
All right.
Where are we at here.
Are backing our extinction.
Say I put the else statement in here to know
OK.
So now we've got our turn off and start functions here and they're implemented.
So sports car and semi truck are going to get those automatically because we conform to the vehicle
protocol.
Now I'm going to go in here and change up the description just a little bit and we're just going to
return self
self dot.
See you on one second.
Let you know actually let's do something different here.
It was come back up here and let's say it's something else in our expansion.
So let's add another property and we'll just call this make model
and we'll just return
we'll just return and make in a model that way whatever we get we'll just pass that in the description
here.
So we'll just for the description which is return self make model and then we'll do the same thing down
here in the in the big league we'll just say return self to make model.
All right.
Now let's come back down here to where we declare these let's call it an and this and you'll see.
Now I've got an auto complete suggestion what we need put there.
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
sports car so Porsche models say 9 11.
The name will come semi truck here.
We're getting the same thing not running.
This is a Peter real and it's virago.
I have no idea if that's a real truck or not but we're going to go with it.
All right.
In the end at that point let's go ahead and comment.
These And so you see now are start methods are retired in the make and model and fired up make and model
fired up and we still blow in the air horn because it's implemented just in a semi truck class
and will turn off.
And now the Peterbilt RAGO shut down
and for here let's pretend we're cool dot make model.
All right.
So we can see here they are right now is putting out a Porsche 911 and appear before Argo.
So that's kind of the power of that protocol extensions bring you.
You can actually implement functions and computed properties in your extensions and then anything that
that conforms to those two that protocol will automatically get that functionality.
This is just kind of a brief overview of it but I think you could probably see the power
so it's real similar to an extension of a type which if you'll recall
let's just extend it double.
So we're going to extend it and we're going to give it a dollar string.
We're going to retire return
string format and we're going to say a dollar sign and then we want to pad with zeros to places and
we want to sell.
So we've just extended a double here.
Now this is a protocol extension this is a this is a top extension which you can do the same sort of
thing any protocol that's in the standard library or any protocol that that you implement yourself.
You can extend it in this way.
So in this case right here we could say our per seat equals let's just say thirty two point one five
times zero point one five Ok so we got four point eight two to five and we want to express that as a
dollar.
So all we've got to do now that we have that extension is percent dollar.
And now you see
I left out the Persius or so now you can see that just by adding this little extension here.
Any double top now that we use from this point forward we'll have this functionality and it works the
same way with it with protocols.
So any extension that you add all of your types that conform to that protocol get that functionality
just lock in this type extension.
All doubles from now on to my program or my application will have the ability to call dollar string
and print out a nicely formatted dollar amount.
So I know this is a fairly short lesson here but I think it pretty well demonstrated the power of protocols
protocol extensions and in the next lesson we're going to go for generics briefly kind of like this
and then after that we'll get into some real world practical
uses for protocols and protocols in a program.
So until next time we'll see.