It's interesting to see that we can now declare return values that are protocols with associated types.
The following throws an error and I don't understand the error.
protocol Plant: Equatable {
var maintenance: String { get }
}
struct Rose: Plant {
let height: Float // inches
let maintenance = "Water once a day."
}
struct Orchid: Plant {
let height: Float // inches
let maintenance = "Water once a month."
}
func makePlant1() -> some Plant {
Rose(height: 4)
}
func makePlant2() -> some Plant {
Orchid(height: 8)
}
let plant1: some Plant = makePlant1()
let plant2: some Plant = makePlant2()
func asdf() {
let condition: Bool = (plant1 == plant2) // error: ambiguous without more context
print(condition)
}
When you declare a function to return
some Plant
then the compiler will look at the content of the function to see what real type is returned and in your example you are returning different types,Rose
andOrchid
.This breaks the requirement from
Equatable
thatSelf
must be the same, that you usesome
doesn't mean that this requirement disappears.The error message you get is a bit confusing but if you change it to
the error is more informative
To be able to use
==
here you need to use objects created by the same functionHere is an informative article on the topic from hackingwithswift.com