Ch 21 Generics, Listing 21.29 Causes eat method to error

func eat<T: Food>(_ food: T) {
    print("I sure love \(food.menuListing).")
}

eat(Bread())

struct Restaurant {
    
    private struct SlicedFood<Ingredient: Food>: Food {
        var food: Ingredient
        var menuListing: String {
            "a slice of \(food.menuListing)"
        }
    }
    
    private struct CookedFood<Ingredient: Food>: Food {
        var food: Ingredient
        var menuListing: String {
            "\(food.menuListing), cooked to perfection!"
        }
    }
    
    func makeSlicedBread() -> Food {
        return SlicedFood(food: Bread())
    }
    
    func makeToast() -> Food {
        let slicedBread = SlicedFood(food: Bread())
        return CookedFood(food: slicedBread)
    }
}

let restaurant = Restaurant()
let toast = restaurant.makeToast()
eat(toast) 

After making the changes in listing 21.29, that last line of code eat(toast) errors:
Value of protocol type ‘Food’ cannot conform to ‘Food’; only struct/enum/class types can conform to protocols.

I’ve gone over this a few times and nothing is jumping out at me. Did others get this working?

I see the same error. Adding the changes in listing 21.30 fixes it. It’s not entirely clear to me from the paragraphs following listing 21.30 whether the code in 21.29 is supposed to work as-is.

1 Like