UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to fix the error “protocol can only be used as a generic constraint because it has Self or associated type requirements”

Swift version: 5.6

Paul Hudson    @twostraws   

Protocols with associated types are a powerful, if somewhat treacherous, feature of Swift. Sometimes it’s fair to say that the only winning move is not to play – i.e., to avoid them entirely – but if that isn’t the case you are sometimes likely to find yourself facing a difficult error: “protocol can only be used as a generic constraint because it has Self or associated type requirements.”

As an example, here’s a protocol with an associated type:

protocol Identifiable {
    associatedtype ID
    var id: ID { get set }
}

So, whatever type wants to conform to Identifiable must state which type they use to identify themselves. We could create two instances of such types like this:

struct Person: Identifiable {
    var id: String
}

struct Website: Identifiable {
    var id: URL
}

That is, people identify themselves using a String, and websites use a URL. So far, so easy. However, if you want to write a function using Identifiable as parameters you’ll hit a problem. For example, you might try to write a function that compares two instances of Identifiable like this:

func compareThing1(_ thing1: Identifiable, against thing2: Identifiable) -> Bool {
    return true
}

That will issue the error “protocol 'Identifiable' can only be used as a generic constraint because it has Self or associated type requirements.”

The reason for the error is simple enough: although thing1 and thing2 being passed into the function both conform to Identifiable that doesn’t make them usable in the same way – the id of a person and the id of a website are completely different types, so there’s no meaningful way you can use them together.

As the error says, this protocol can be used only as a generic constraint. That’s actually pointing us to the solution here: if we use Identifiable as a generic constraint then we can tell Swift not only that thing1 and thing2 conform to the protocol but also that they are actually the same type.

func compareThing1<T: Identifiable>(_ thing1: T, against thing2: T) -> Bool {
    return true
}

That code fixes the problem, because Swift has enough information to know how you plan to use thing1 and thing2.

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 8.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.2/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.