Type inference issue with map and filter chained

Hello everyone, I found this on StackOverflow : https://stackoverflow.com/questions/46381752/swift-4-methods-chaining/

Is this a bug of Swift 4 or is this normal ? I don't understand why the problem only happens when methods are chained !

Thank you

Trevör

Is this a REPL-only issue perhaps? The code below compiles `swift build` without error for me (pasted into Sources/main.swift, Ubuntu 17.04, Swift 4 release; haven’t tried in Xcode).

A similar issue, but apparently not REPL-only:
https://bugs.swift.org/browse/SR-1856

Regards,
Will Stanton

Code compiles fine:
class test {
    let id = 11
}

var dict: [Int: test] = [10: test()]

let filtered = dict.filter({ $0.value.id > 10 })
let sorted = filtered.sorted(by: {$0.value.id > $1.value.id })

print(sorted)

let filteredAndSorted = dict.filter({ $0.value.id > 10 }).sorted(by: {$0.value.id > $1.value.id })
print(filteredAndSorted)

···

On Sep 24, 2017, at 4:52 AM, Trevör ANNE DENISE via swift-users <swift-users@swift.org> wrote:

Hello everyone, I found this on StackOverflow : https://stackoverflow.com/questions/46381752/swift-4-methods-chaining/

Is this a bug of Swift 4 or is this normal ? I don't understand why the problem only happens when methods are chained !

.

It’s a bug.

The reason you see this when they are chained is because the type checker (in general) often finds multiple potential solutions (meaning a set of types and particular function overloads) and then has to select the “best” solution from those. For example one solution might involve “map” from type A combined with “filter” from type B, and another might involve “map” from type X and “filter” from type Y.

For the sake of illustration, let’s say it found only those two solutions in your case. Selecting the best solution involves examining the components of the solutions and comparing each component to determine which is best. In your example we may decide A.map is the best map, but Y.filter is the best filter. There is no solution that involves A.map combined with Y.filter, so we consider the solution to be ambiguous.

The combination of the type checker and standard library design should ensure this never happens, but we have some known bugs we need to work through in order to fix all of these cases.

Mark

···

On Sep 24, 2017, at 1:52 AM, Trevör ANNE DENISE via swift-users <swift-users@swift.org> wrote:

Hello everyone, I found this on StackOverflow : https://stackoverflow.com/questions/46381752/swift-4-methods-chaining/

Is this a bug of Swift 4 or is this normal ? I don't understand why the problem only happens when methods are chained !

Is this a REPL-only issue perhaps? The code below compiles `swift build` without error for me (pasted into Sources/main.swift, Ubuntu 17.04, Swift 4 release; haven’t tried in Xcode).

A similar issue, but apparently not REPL-only:
https://bugs.swift.org/browse/SR-1856

Regards,
Will Stanton

Code compiles fine:
class test {
     let id = 11
}

var dict: [Int: test] = [10: test()]

let filtered = dict.filter({ $0.value.id > 10 })
let sorted = filtered.sorted(by: {$0.value.id > $1.value.id })

print(sorted)

let filteredAndSorted = dict.filter({ $0.value.id > 10 }).sorted(by: {$0.value.id > $1.value.id })

Just checked:

XCode 9: "Ambiguous use of 'filter'" error for this line

Vladimir.

···

On 03.10.2017 21:32, Will Stanton via swift-users wrote:

print(filteredAndSorted)

On Sep 24, 2017, at 4:52 AM, Trevör ANNE DENISE via swift-users <swift-users@swift.org> wrote:

Hello everyone, I found this on StackOverflow : https://stackoverflow.com/questions/46381752/swift-4-methods-chaining/

Is this a bug of Swift 4 or is this normal ? I don't understand why the problem only happens when methods are chained !

.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users