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
Of course I found the solution immediately after posting this 🤦♂️
The trick is to get the parameter pack into the return value somehow. I did it using a typealias
inside the generic type:
struct Tweak<Value> {
typealias Name = String
var name: Name
var value: Value
}
func names<each Value>(of tweak: repeat Tweak<each Value>) -> (repeat Tweak<each Value>.Name) {
return (repeat (each tweak).name)
}
And now it works as it should 🙂