[swift-evolution] Would having "MyType.Protocol" to indicate a type's protocols mess anything up?
Daryle Walker
darylew at mac.com
Sun Jul 2 20:49:06 CDT 2017
> On Jul 1, 2017, at 2:07 AM, Brent Royal-Gordon <brent at architechies.com> wrote:
>
>> On Jun 30, 2017, at 6:17 PM, Daryle Walker via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>> Given a type MyType, how can I get a type-alias to the type’s protocols? If MyType conforms to Protocol1 and Protocol2, I would want something like
>>
>> typealias MyProtocol = Protocol1 & Protocol2
>>
>> (and Any if MyType doesn’t conform to any protocols). Does this facility already exist in Swift? I don’t think it does, so I proposed the hybrid “MyType.Protocol” syntax to express the idea.
>
> Leave the syntax aside. What are you planning to do with this feature? I understand that you want to have some way of saying "composition of all protocols this type conforms to"; I don't understand *why* you want it or what kind of code you would need it for.
It wasn’t until I started this thread that I realized that Swift didn’t have a way to get a bulk list of a type’s protocols. A little later, I came up with a reason why this never came up before (or at least not often): knowing this isn’t really useful.
You could use this to make MyTypeB conform to all protocols of MyTypeA. But then you’re stuck. The list of protocols for a type is a closed from the linker’s perspective, but open from the user’s perspective. The user couldn’t write all the methods needed unless the unknown ones all have default implementations. The proposal I’m coming up with has a facility to copy all the members of one type to another. You can specify by exact member name, or you can specify a protocol to get copies of all applicable members at once. This is where an all-protocols alias can help. With manual copying, the user’s cardinality of members copied is usually less than what’s available, with an automatic list the cardinalities are always equal.
—
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170702/5b57f36a/attachment.html>
More information about the swift-evolution
mailing list