WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Allow Protocols to be Nested in Non-Generic Contexts

Available from Swift 5.10

Paul Hudson      @twostraws

SE-0404 allows us to create nested protocols, meaning that we can place protocols inside structs, enums, classes, actors, and even functions, with the sole restriction that whatever we're nesting the protocol in can't use generics.

This is particularly helpful when common names are given to protocols. For example, the word "transaction" could feasibly be used to mean an animation transaction, a bank transaction, and a database transaction all in the same app.

One way to resolve this is by using compound names – we add more words to protocol names to clarify what we mean, like this:

protocol AnimationTransaction {
}

protocol BankTransaction {
}

protocol DatabaseTransaction {
}

Another common problem also occurs when several similar protocols exist. For example, in SwiftUI we have protocols for ButtonStyle, LabelStyle, ListStyle, and more, all encapsulating the idea that a view can be styled in various ways.

Both of these can be resolved with this change. In the case of transactions, we could nest each transaction type inside whatever type it operated on:

struct Animation {
    protocol Transaction {
    }
}

struct Bank {
    protocol Transaction {
    }
}

struct Database {
    protocol Transaction {       
    }
}

Where those protocols are used externally, they would now be written Animation.Transaction, Bank.Transaction, and Database.Transaction, but inside their respective structs they can just be referred to as Transaction.

In theory, SwiftUI could also move to Button.Style, List.Style, and so on, but that feels like a big change at this point.

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Other changes in Swift 5.10…

Download all Swift 5.10 changes as a playground Link to Swift 5.10 changes

Browse changes in all Swift versions

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.