Chapter 12, ResourceType, where keyword

In the following code in Chapter 12, can someone explain the where keyword? Is this used specifically with generic types? Is this saying "where the ResourceType conforms to Codable? So is this saying the struct parameter doesn’t care what class ResourceType is as long as it meets the minimum requirement of conforming to codable? What happens if the ResourceType doesn’t conform to Codable? Thanks in advance. Great book.

struct ResourceRequest<ResourceType>
  where ResourceType: Codable {
  // 2
  let baseURL = "http://localhost:8080/api/"
  let resourceURL: URL

  // 3
  init(resourcePath: String) {
    guard let resourceURL = URL(string: baseURL) else {
      fatalError("Failed to convert baseURL to a URL")
    self.resourceURL =

Yes correct, ResourceRequest will work with any ResourceType that conforms to Codable. If it doesn’t conform to Codable it simply won’t compile.


let resourceRequest = ResourceRequest<SomeNonCodableType>(resourcePath: "notCodable")

will fail compilation with something along the lines of SomeNonCodableType does not satisfy the generic constraints.

You can read more here Generics — The Swift Programming Language (Swift 5.5)