Tuple conversion from '(key: KeyType, value: ValueType)' to '(foo: KeyType, bar: ValueType)' mismatches labels

I have a dict of [KeyType: ValueType], in a reduce call I use more informational labels to make code more clear, like this:

dict.reduce(initialResult) { (result, item: (foo: KeyType, bar: ValueType)) -> ResultType in
    3 lines of code where calling foo `foo` and bar `bar` makes things clearer than calling foo `item.key` and bar `item.value`
}

It builds fine before, but since some version of Xcode/Swift it triggers this warning:

Tuple conversion from '(key: KeyType, value: ValueType)' to '(foo: KeyType, bar: ValueType)' mismatches labels

What's the best solution? Thanks.

1 Like

yes, since Swift 5.6

Just don't do that? :slight_smile:

Will be shorter as well:

dict.reduce(initialResult) { result, item in
    result + calculateResult(foo: item.key, bar: item.value)
}
1 Like

I was trying to condense the sample code but that obscured the reality. I updated the post. The closure has 3 lines of code where calling foo foo and bar bar makes things clearer than calling foo item.key and bar item.value.

dict.reduce(initialResult) { (result, item) -> ResultType in
    let item = item as (_, _) as (foo: _, bar: _)
    //3 lines of code where calling foo `foo` and bar `bar` makes things clearer than calling foo `item.key` and bar `item.value`
}
1 Like

Or just this:

dict.reduce(initialResult) { result, item in
    let (foo, bar) = (item.key, item.value)
    //3 lines of code where calling foo `foo` and bar `bar` makes things clearer than calling foo `item.key` and bar `item.value`
}

dict.reduce(initialResult) {
    let (foo, bar) = ($1.key, $1.value)
    //3 lines of code where calling foo `foo` and bar `bar` makes things clearer than calling foo `item.key` and bar `item.value`
}

Edit: or this in fact. Didn't realise this is possible:

dict.reduce(initialResult) { result, item in
    let (foo, bar) = item
    //3 lines of code where calling foo `foo` and bar `bar` makes things clearer than calling foo `item.key` and bar `item.value`
}

dict.reduce(initialResult) {
    let (foo, bar) = $1
    //3 lines of code where calling foo `foo` and bar `bar` makes things clearer than calling foo `item.key` and bar `item.value`
}

The added benefit - you'd be using shorter foo and bar instead of item.foo and item.bar

3 Likes

Cool, I like this best, thanks!