// ❌: `x`ͱ͍͏ϥϕϧͳ͍ case .lambda(parameters: let params, body: let body) // ⭕ case .lambda(params: let params, body: let body) // ❌: ୈҰҾϥϕϧ͕ؒҧ͍ͬͯΔ •ύλʔϯͷҰ؏ੑ indirect enum Expr { case variable(name: String) case lambda(parameters: [String], body: Expr) } ϥϕϧ໊౷Ұ͢Δඞཁ͕͋Δ
Person { var name: String var friends: [Person] = [] var bestFriend: Person? = nil init(name: String) { self.name = name } } var han = Person(name: "Han Solo") var luke = Person(name: "Luke Skywalker") luke.friends.append(han)
create a key path and use it let firstFriendsNameKeyPath = \Person.friends[0].name let firstFriend = luke[keyPath: firstFriendsNameKeyPath] // "Han Solo" // or equivalently, with type inferred from context luke[keyPath: \.friends[0].name] // "Han Solo" // rename Luke's first friend luke[keyPath: firstFriendsNameKeyPath] = "A Disreputable Smuggler" // optional properties work too let bestFriendsNameKeyPath = \Person.bestFriend?.name let bestFriendsName = luke[keyPath: bestFriendsNameKeyPath] // nil, if he is the last Jedi \Λ͚ͭΔ͜ͱͰɺKeyPathͱͯ͠͏͜ͱ͕Ͱ͖Δ
initial: A, _ combine: (inout A, Iterator.Element) - > ()) -> A { var result = initial for element in self { combine(&result, element) } return result } } inoutΛͬͨreduce͕Ճ͞ΕΔ