rayx
(Huan Xiong)
23
Hi, Dave, I have two questions on the code. They are mostly implementation details.
-
You use the array index as the value of PersonID. I understand that makes it fast to retrieve a Person value by its ID (I suppose that's why you do it this way), but doesn't that makes PersonID value coupled with the storage format? It seems a bit inflexible to me.
-
Is there any reason why you don't put friends in the Person struct? In practice, a Person may have many properties (e.g., tweets, likes, etc.). I think it's natural to put them in the struct?
That said, if friends is moved to the struct and suppose we would like to implement a method, say, getFriendNames(), for Person, then the method would need to take a closure to translate PersonID to name. This is a minor example why I said value type often required functional programming techniques.
// Suppose friendIDs is moved to Person struct
extension Person {
func getFriendNames(_ idResolver: (PersonID) -> Person) -> [String] {
return friendIDs.map { idResolver($0).name }
}
}