Pick the fix
You need a function that saves a User to any store whose Item is User. Which signature?
protocol Store<Item> {
associatedtype Item
func save(_ item: Item) throws
}Hint 1 of 1
How do you say 'a Store of Users'?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A func persist(_ u: User, in store: some Store<User>) with protocol Store<Item>
Correct. The primary associated type pins Item to User.
Why not the others?
- B
func persist(_ u: User, in store: any Store). Item is unknown, sostore.save(u)won't type-check. - C Cast with
store.save(u as! Store.Item). You can't name the associated type like that, and force casts crash. - D Declare
associatedtype Item = Any. That throws away type safety for every conformer.