Łukasz Mróz
5 min readMar 28, 2016

Hey there! I moved my blog to http://sunshinejr.com
There will be a lot of stuff about iOS and Open Source in general, so if you are interested in these, please give it a shot ;-)

Swift talks: What do you really know about typealias and associatedtype?

Note: This post was written way back when Swift 2.2 was a thing. If you are still reading this one, please keep in mind that this is some sort of a history and rather a note than a useful post for you. Thanks!

After the Swift 2.2 update we’ve heard that typealias is deprecated in protocols and we shall use associatedtype instead. But how many of you really knew the purpose of this change (besides that it was “confusing”)?

Let’s move back to the time when there was no associatedtype keyword and we have to use typealias everywhere.

February, 2016.

“The Office”

A casual day in the office. We have to make a struct that will use Double type for currency. This struct will have many methods that will use this type, so we heavily rely on it. We don’t want to make a mistake and use Float in any of the method/properties, so we’re gonna make a typealias for it.

Easy. Now we are safe and instead of Double we will write Currency type, which is just an alias for Double (when we decide to use Int for Currency, it won’t be a hard task as well). Let’s move on.

Now we have a task to make another struct that will have to use Currency type as well. It will be kind of similar, but not really. We can’t really use inheritance with structs and this guy told me that protocols are cool, so let’s make a protocol!

Now we just need to conform to it. We will also write a function that will use this Currency type, to make sure it works:

Voila! It works! Perfect. We call it a day.

Oh but there is this one task where we only need to connect a collection of Currency types to a table view… That should be really easy, two lines of code I believe… Okay, this one last task and we’re out.

So first we will create an array of CurrencyProtocol:

Aaaand boom. We’ve got an error.

Whaaaaat? Why is it screaming at me! It is just a collection of protocol types! Apple told me it was available in Swift!

Hang on just a minute there, monkey boy.

Let’s look at the error. What does it say?

“Protocol ‘CurrencyProtocol’ can only be used as a generic constraint because it has Self or associated type requirement.”

Oh boy. But how? We just made a protocol with typealias, that’s it!

As it turns out, we didn’t. What we actually did was a generic protocol. The typealias in our protocol means something completely different that you would think it does. We actually created an associated type with a default value in it. Basically we made a generic protocol CurrencyProtocol with a type Currency associated to it, which by default is assigned to Double, (but can be overridden). This shows us that typealias really has more than only one meaning. These are:

  1. Actual alias for a type.
  2. Associated type in protocol.

To create a generic protocol you can use Self in it, or add associated type to it. But with adding associated type you can also specify default type, which really confuses at first. So how would we implement the protocol with typealias (and not an associated type) then? Well, quite simple, but not that straight-forward as you would think:

In extensions typealias means basically what it means in structs and so on. It is just an alias for a name - which is what we want in our case. When we implement the code above the error should be gone.

Back from the nightmare.

Whoah, that wasn’t a fun time, was it? Let’s relax a little bit.

Swift 2.2 introduced associatedtype to us, which should fix the problem… of a confusion. Because the rest stays the same, just the keyword typealias is deprecated in protocols and instead we should use the new associatedtype one. But now you know the difference and how to create an actual typealias instead of associated type.

Example of default associatedtype.

So we’ve talked about the default value for associatedtype and that it is quite simple to override it. I’ve made an example where I show how you can use the default value and also how can you override it:

Quite simple after the typealias introduction. The tricky part is our own TypedItemType, that we return in methods instead of ItemType, that is the default associatedtype. Because ItemType doesn’t have any requirements (like conforming to X protocol) we can override the returned type for our own generic associated type (which is completely unrelated to Int, which is default type). As a result it overrides the whole associatedtype default value. If the associatedtype needed to conform to e.g. IntegerType, we would have to make the same requirement in our own type (instead of conforming to StringLiteralConvertible, which doesn’t need to be there anyways).

The end!

Aaaaand that’s it! Thank you for reading. Full playground for playing with typealias and associatedtype is here.