[swift-evolution] [Discussion] Generic protocols
Anton Zhilin
antonyzhilin at gmail.com
Sat Dec 10 04:31:07 CST 2016
I have to agree with Tino Heth, our aim is to add something basic at first.
Although we’d better make sure that we will be able to add other wanted
features in the future.
How about this plan?
1. Generic protocols without associated types
2. Basic conflict resolution using “dot” syntax
3. Generic protocols with associated types (depends on (2))
4. Specialization of non-generic protocols with associated types (+work
on existential notation)
Some inline reply follows.
While trying to find the talk about overlapping protocol members from
> different protocols I spotted your proposal:
> https://github.com/Anton3/swift-evolution/blob/generic-proto
> cols/proposals/NNNN-generic-protocols.md
>
This is quite an old listing. Thanks for reminding!
Here is the evolution-thread: https://lists.swift.org/piperm
> ail/swift-evolution/Week-of-Mon–20160919/027122.html
> <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160919/027122.html>
>
> The suggestion there was to prefix everything with the corresponding
> protocol when there is a collision.
>
> struct Test : Foo<Int>, Foo<String> {
> init(_ value: Foo<Int>.Value) { … } // or better: init(_ value: Int)
> init(_ value: Foo<String>.Value) { … } // init(_ value: String)
>
> // assume both had the function `foo()`
> func Foo<Int>.foo() { … }
> func Foo<String>.foo() { … }
> }
>
> That’s a working solution. It doesn’t scale very much, if we try to apply
it to protocol inheritance (conformance) hierarchies, but we shouldn’t care
for now.
Is there really a need for true generic protocols? I’m still
fascinated by the idea how a generic and constrained typealias could
create this new ‘pseudo’ generic protocol.
>
> I agree that ideally, existing protocols should be intact. If so, then we
should consider “the original protocol with associated types” and
“explicitly specialized protocol” as two different protocols:
protocol Constructible {
associatedtype Value
func foo()
}
struct MyStruct : Constructible {
typealias Value = Int
func foo() { }
}
extension MyStruct : Constructible<String> { // or
Constructible<Value: String>, or whatever
typealias Constructible<String>.Value = String
func Constructible<String>.foo() { }
}
MyStruct.Value // IntMyStruct.Constructible<String>.Value // String
func bar<T: Constructible>(t: T) // can call with MyStructfunc bar<T:
Constructible<Int>>(t: T) // can NOT call with MyStructfunc bar<T:
Constructible<String>>(t: T) // can call with MyStruct
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161210/d809430e/attachment.html>
More information about the swift-evolution
mailing list