Generalize APIs with parameter packs

RSS for tag

Discuss the WWDC23 Session Generalize APIs with parameter packs

View Session

Posts under wwdc2023-10168 tag

4 Posts
Sort by:
Post not yet marked as solved
4 Replies
723 Views
Consider this example: struct IdentifiedValue<T> { typealias ID = UUID var id: ID var value: T } class ValueStore { func add<T>(_ value: T) -> IdentifiedValue<T>.ID { let id = nextID() let identifiedValue = IdentifiedValue(id: id, value: value) // Do something with identifiedValue… return id } } What this add(_:) method does is that it gets a UUID from somewhere, passes it into the IdentifiedValue initializer and then return that ID. What is the best way to turn this add(_:) into a method that takes multiple parameters? I can’t just add the each and repeat keywords because then the return statement isn’t correct (obviously): func add<each T>(_ value: repeat each T) -> (repeat IdentifiedValue<each T>.ID) { let id = nextID() let identifiedValue = repeat IdentifiedValue(id: id, value: each value) // Do something with identifiedValue… return id // error } The only way I found was to get everything into a single line so the repeat and each keywords act on the whole line. This means adding a separate method that does the thing for a single value and calling that. At first, I tried adding both of the add(_:) variants and implementing the one that takes a pack by calling the one that takes a single value, but then the compiler says it doesn’t know which version of add(_:) to call. I could rename one of the two but I ended up using an inner function (where it isn’t confused, I guess due to shadowing) like this: func add<each T>(_ value: repeat each T) -> (repeat IdentifiedValue<each T>.ID) { func add<T>(_ value: T) -> IdentifiedValue<T>.ID { let id = nextID() let identifiedValue = IdentifiedValue(id: id, value: value) // Do something with identifiedValue… return id } return (repeat self.add(each value)) } So my question is: Is there way to write this without a second function?
Posted Last updated
.
Post marked as solved
2 Replies
875 Views
I have this code: struct Tweak<Value> { var name: String var value: Value } func name<Value>(of tweak: Tweak<Value>) -> String { return tweak.name } func names<Value1, Value2>(of tweak1: Tweak<Value1>, _ tweak2: Tweak<Value2>) -> (String, String) { return (tweak1.name, tweak2.name) } Now I want to implement the second function using parameter packs: func names<each Value>(of tweak: repeat Tweak<each Value>) -> (repeat String) { return repeat each tweak.name } But that gives this error: error: pack expansion 'String' must contain at least one pack reference func names<each Value>(of tweak: repeat Tweak<each Value>) -> (repeat String) { ^~~~~~~~~~~~~ What is the correct syntax for doing this? Or is it impossible to spell this? Thanks, Marco
Posted Last updated
.
Post not yet marked as solved
0 Replies
805 Views
The sample code below does not compile: protocol RequestProtocol { associatedtype Input associatedtype Output func evaluate(_: Input) -> Output } struct Evaluator<each Request: RequestProtocol> { let item: (repeat each Request) func query(_ input: repeat (each Request).Input) -> (repeat (each Request).Output) { return (repeat (each item).evaluate(each input)) } } Code causes two compiler errors: 'each' cannot be applied to non-pack type '(repeat each Request)' Pack expansion requires that 'each Request' and '()' have the same shape Please advise.
Posted Last updated
.
Post marked as solved
1 Replies
932 Views
Code below from the video: Generalize APIs with parameter packs protocol RequestProtocol { associatedtype Input associatedtype Output func evaluate(_ input: Input) -> Output } struct Evaluator<each Request: RequestProtocol> { var item: (repeat each Request) func query( _ input: repeat (each Request).Input ) -> (repeat (each Request).Output) { return (repeat (each item).evaluate(each input)) } } The Evaluator declaration causes a compiler error, "Generic types with parameter packs are experimental". Is there a switch or flag to enable use of parameter packs?
Posted Last updated
.