Generalize APIs with parameter packs

RSS for tag

Discuss the WWDC23 Session Generalize APIs with parameter packs

Posts under wwdc2023-10168 tag

4 Posts

Post

Replies

Boosts

Views

Activity

Is it possible to `repeat` statements that do not reference a parameter pack?
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?
4
0
1.2k
Jun ’23
Can a function with parameter packs return a non-generic value for each parameter?
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
2
1
1.5k
Jun ’23
Sample Code from WWDC video does not compile.
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.
0
2
1k
Jun ’23
Error: "Generic types with parameter packs are experimental"
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?
3
0
1.4k
Jun ’23
Is it possible to `repeat` statements that do not reference a parameter pack?
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?
Replies
4
Boosts
0
Views
1.2k
Activity
Jun ’23
Can a function with parameter packs return a non-generic value for each parameter?
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
Replies
2
Boosts
1
Views
1.5k
Activity
Jun ’23
Sample Code from WWDC video does not compile.
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.
Replies
0
Boosts
2
Views
1k
Activity
Jun ’23
Error: "Generic types with parameter packs are experimental"
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?
Replies
3
Boosts
0
Views
1.4k
Activity
Jun ’23