Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Much like we can define generic types and functions, we can also create generic requirements in our protocols. This isn't as straightforward as it seems however and in this video lets take our first look at what an associated type is and how we implement it
-
0:00
[MUSIC]
-
0:04
In Swift, there's several different ways to codify or represent information and
-
0:10
logic, functions, types and interfaces or protocols.
-
0:15
We've seen so far that functions and types can be made generic.
-
0:19
So what about protocols?
-
0:21
You might think of a protocol as already something generic,
-
0:24
in that any type can conform to a protocol.
-
0:27
But what about the internals of a protocol?
-
0:30
Can we define a protocol where the requirements themselves are generic?
-
0:35
We can, but not in the same way as we've seen so far.
-
0:39
With protocols, we have a different concept called associated types.
-
0:44
So if a new playground page open here and
-
0:47
we're going to model a new data structure in this video.
-
0:50
Except we're not going to provide a full implementation like we just did.
-
0:54
We'll start by defining an interface through a protocol.
-
0:57
So we'll call this protocol Stack.
-
1:00
A stack is essentially an array with limited functionality.
-
1:04
You can push a new element to the top of a stack pop to remove the element
-
1:09
from the top and peek at the top element without popping it.
-
1:13
The main point of this stack is the order.
-
1:16
A stack operates on last in, first out order.
-
1:19
The element you most recently pushed is the one you get back when popped.
-
1:24
Let's defined this interface.
-
1:25
So in here, first step is the push function.
-
1:28
Assuming we set up the stack using a struct,
-
1:31
like an array, then push will be a mutating method.
-
1:34
And we need to indicate that to the compiler.
-
1:36
So we'll start with mutating func push.
-
1:39
The method is going to take a single argument,
-
1:42
an element to push on to the stack.
-
1:47
Now here's the tricky part.
-
1:49
What type do we give this method?
-
1:51
Remember, we want this method to have a generic requirement.
-
1:55
Any concrete type of stack that we need to create needs to be able to conform to this
-
1:59
protocol and work despite it containing strings, or integers, or whatever.
-
2:04
The end goal is that each stack class or
-
2:07
struct that we create has to contain one type only.
-
2:10
What we need here is an associated type.
-
2:13
So right above the function, let's type out the keyword
-
2:16
associatedtype followed by a name we want to give this associated type.
-
2:22
This name is just like how we name our type parameters in a generic function.
-
2:27
So we'll call this element.
-
2:29
And now back in the function, we can specify the type as a type of argument.
-
2:36
Just like type parameters, associated types give a placeholder name to a type
-
2:41
that is used as part of the protocol.
-
2:43
The actual type to use for
-
2:45
that associated type is not specified until we actually adopt the protocol.
-
2:51
Okay, so let's add two other requirements.
-
2:53
Pop returns the last element we pushed onto the stack
-
2:57
which also mutates the stack.
-
2:58
So we'll say mutating func pop and this is going to return the last element.
-
3:04
Finally, we have peak which lets us look at the very last element without popping
-
3:09
it and we'll call this top.
-
3:14
Pretty simple.
-
3:15
Now let's see how we can go from an associated type to more concrete usage.
-
3:20
There are two ways we can do this.
-
3:23
So we'll declare a stack of ints, so
-
3:25
I'll call a struct IntStack and we'll conform to the stack protocol.
-
3:32
The first way to satisfy the associated type requirement is to be explicit and
-
3:37
state it in code.
-
3:39
We do this by using the type alias keyword and
-
3:42
indicating that element which is the associated type in our protocol
-
3:47
is a type alias for a concrete type inside this object.
-
3:50
So we can say typealias or if you start typing at Element,
-
3:54
it should say Element equal Type and add that typealias keyword automatically.
-
3:59
And you will see that element is always going to be an integer inside this struct.
-
4:04
Now all the methods push and
-
4:06
pop take integers instead of our generic requirement element.
-
4:11
Let's provide a really quick implementation.
-
4:13
We'll use an underlying array for storage and make it private, so let's say private
-
4:18
var array and this is an array that is generic over int.
-
4:25
And for the implementations of the method, we'll just use this array, so
-
4:29
push just appends it to that array that we just created.
-
4:37
Pop removes and returns the last element from the array.
-
4:41
So we'll say return pop array.popLast.
-
4:45
And finally, top just reads the last element in the array.
-
4:53
Pretty simple except we seem to have some errors, what's going on?
-
4:57
All right, we need to make this mutating and here as well,
-
5:02
we still seem to have this error, so let's check.
-
5:07
Okay, this seems to be a problem with my playground.
-
5:10
It should work on your end,
-
5:12
let's quickly go back to a different page and see if we can make it work.
-
5:22
Okay, now I said there were two ways of doing this and the second way is to simply
-
5:27
let the compiler infer the type for the associatedtype requirement.
-
5:32
By specifying the argument type or the return type for
-
5:36
all the methods as integer, the compiler can infer that we've substituted int for
-
5:41
the associatedtype because those say element over here.
-
5:44
So we can just get rid of this typealias declaration and
-
5:48
everything should still work.
-
5:51
So this is how you give the protocol generic requirements.
-
5:54
This seems pretty simple but we can actually do quite a bit with this.
-
5:58
Before we go down that road though, let's learn a bit more about type constraints.
You need to sign up for Treehouse in order to download course files.
Sign up