Type aliases and protocol changes

Type aliases are named types that fill in for existing types in Swift. Once defined, you can use these types anywhere in your code. Swift 3 now supports type aliases based on generics. In addition, type aliases are now supported for protocols and protocol extensions. Speaking of protocols, Swift 3 made a change to protocol use that makes things simpler and paves the way for expected future changes to this feature. Let's add the new changes more closely and work through some examples.

Generic type aliases are a new addition for Swift 3. As a reminder, a type alias is a way to declare a named alias for an existing type in the language. After you create your named alias, you can use the aliased type in your code just as you would any other type. Generic type aliases allow you to add type parameters that can be used in defining a generic type. Let's consider a few examples to show the new possibilities you have with creating type aliases in Swift 3:

    typealias ScoreBag<T> = [T]
    typealias TriplePointTuple<T> = (T,T,T)
    typealias AddPlotter<X:Hashable,Y> = Dictionary<X, Y>
    typealias UndoItem<T> = [Date:T]

In prior versions of Swift, you needed to use the protocol<...> syntax when you were defining a type that adhered to multiple protocols. In Swift 3, you now use & between each protocol that your type adopts. This is merely syntactical sugar today, but will serve as the base for defining more generic types in future additions of Swift.

In Swift 2:

protocol Driving {} 
protocol Parking {} 
protocol Braking {} 
struct Car: Driving, Parking, Braking {} 
let zoomzoom: protocol<Driving, Braking, Parking> = Car() 

In Swift 3:

let zoomzoom: Driving & Braking & Parking = Car() 

To better convey the intent of a compound type built from individual protocols, the Swift team now prefers using & to a comma for defining multiple protocols on a type.

Swift 2.2 introduced the associatedtype keyword to handle associated types in protocols. This change removed the confusion around using the typealias keyword because it is now only tasked with defining types. An additional benefit of adding the associatedtype keyword is that it allows us to make type aliases in protocols and protocol extensions that are based on the associated types. Looking at the Sequence protocol from the standard library, we can see that Iterator is defined as an associated type that inherits the IteratorProtocol. With Swift 3, I can now add a type alias named Element that indirectly references an associated type on the IteratorProtocol to make my syntax cleaner. Further, I can use any of the type aliases I create in my protocol with my protocol extensions as well.

In Swift 3:

public protocol Sequence { 
 
    associatedtype Iterator : IteratorProtocol 
    
    typealias Element = Iterator.Element 
 
    public func makeIterator() -> Iterator 
 
   func map<T>(_ transform: (Element) throws -> T)  
rethrows -> [T] 
 
} 

In the preceding Sequence protocol, I can use my type alias Element in the map() function. In previous versions of Swift, you would have had to use Iterator.Element.