Is this a bug in Swift 3 compiler? It's not ambiguous, it's ==
and two String
s.
It says:
error: ambiguous reference to member '=='
let strs = things.filter { return $0.id == "1" } .map { t in
^~
For this example code:
class Thing {
var id: String = ""
}
let things = [Thing]()
let x = 1
let strs = things.filter { return $0.id == "1" } .map { t in
if x == 1 {
return "a"
}
else {
return "b"
}
}
It's a misleading error message – the ambiguity actually lies with the closure expression you're passing to map(_:)
, as Swift cannot infer the return type of a multi-line closure without any external context.
So you could make the closure a single line using the ternary conditional operator:
let strs = things.filter { $0.id == "1"} .map { _ in
(x == 1) ? "a" : "b"
}
Or simply supply the compiler some explicit type information about what map(_:)
is returning:
let strs = things.filter { $0.id == "1"} .map { _ -> String in
let strs : [String] = things.filter { $0.id == "1"} .map { _ in
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With