Roshan
(Roshan)
January 17, 2018, 7:29am
1
Hi,
Here is some sample code that gives a protocol conformance error in a
playground:
protocol A {}
protocol B: A {}
protocol C {
func test(x: A)
}
class M: C {
func test(x: B) {}
}
Is there a reason why the compiler doesn't infer that ((B) -> ())
matches ((A) -> ()) because of inheritance?
···
--
Warm regards
Roshan
Nevin
January 17, 2018, 8:29am
2
Because it would break this:
func foo<T: C, U: A> (c: T, a: U) { c.test(a) }
Nevin
···
On Wed, Jan 17, 2018 at 2:29 AM, Roshan via swift-users < swift-users@swift.org> wrote:
Hi,
Here is some sample code that gives a protocol conformance error in a
playground:
protocol A {}
protocol B: A {}
protocol C {
func test(x: A)
}
class M: C {
func test(x: B) {}
}
Is there a reason why the compiler doesn't infer that ((B) -> ())
matches ((A) -> ()) because of inheritance?
--
Warm regards
Roshan
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users
DeFrenZ
(Davide De Franceschi)
January 20, 2018, 7:37pm
3
In simpler terms: test(x: B)
will work only for B
s, which makes it more restrictive compared to what C
requires (all kinds of A
s).
Maybe you want something more like
protocol C {
associatedtype T: A
func test(x: T)
}
but still introduce associatedtype
s only if strictly necessary :)