[swift-evolution] Proposal for generic protocols

Ken Ferry kenferry at gmail.com
Thu Dec 3 21:31:44 CST 2015


Oops, I messed that up a bit. I meant equivalent codegen to 

protocol Foo {
    func doThing<CB : FooDoThingCallback>(callback:CB)
}

protocol FooDoThingCallback {
    func callback<C : CollectionType where C.Generator.Element == Int>(coll:C)
}

foo.doThing(Callback())
struct Callback : FooDoThingCallback {
    func callback<C : CollectionType where C.Generator.Element == Int>(coll:C) {
        print("got \(coll)!")
    }
}


> On Dec 3, 2015, at 6:59 PM, Ken Ferry <kenferry at gmail.com> wrote:
> 
> How about something along the lines of 
> 
> 	protocol Foo {
> 	    func doThing() -> <C:CollectionType, C.Generator.Element == Int>C
> 	}
> 
> 	let coll : <MyColl>MyColl = foo.doThing()
> 	print("got \(coll)!")
> 
> which compiles to be equivalent to 
> 
> 	protocol Foo {
> 	    func doThing(callback:FooDoThingCallback)
> 	}
> 
> 	protocol FooDoThingCallback {
> 	    func callback<C : CollectionType where C.Generator.Element == Int>(coll:C)
> 	}
> 
> 	    foo.doThing(MyCallback())
>   	struct MyCallback : FooDoThingCallback {
>             func callback<C : CollectionType where C.Generator.Element == Int>(coll:C) {
> 	        print("got \(coll)!")
> 	    }
> 	}
> 
> -ken
> 
>> On Dec 3, 2015, at 6:19 PM, Joe Groff <jgroff at apple.com <mailto:jgroff at apple.com>> wrote:
>> 
>> 
>>> On Dec 3, 2015, at 6:10 PM, Tal Atlas <me at tal.by <mailto:me at tal.by>> wrote:
>>> 
>>> Thanks for the great info. I made this because I didn't see anything on the current plan and I really hope that whatever comes out has the power and ease of the proposal. 
>>> 
>>> Thanks again for the great responses. 
>> 
>> No problem, thank you for writing this up! If you want to think about this more, some things you might consider:
>> 
>> - What changes would be necessary to the standard library to make the most of this feature? Some particular problems we'd like to solve are to eliminate the need for the AnyGenerator/AnySequence/AnyCollection wrapper types, and to provide a solution for heterogeneous equality, so that protocols can inherit Equatable and Hashable without forfeiting the ability to be used dynamically. See Brent Simmons' Swift diary posts at http://inessential.com/swiftdiary <http://inessential.com/swiftdiary> for an example of why the latter is important.
>> - A hybrid approach that allows for both generic parameters and associated types like Rust, as Kevin pointed out, or one where generic parameters are sugar for associated types, might be worth considering.
>> 
>> Thanks again!
>> 
>> -Joe
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151203/66e11ec2/attachment-0001.html>


More information about the swift-evolution mailing list