[swift-evolution] AnyProtocol proposal
João Nunes
joao3001 at hotmail.com
Wed Feb 17 11:59:56 CST 2016
In the example given. How would you restrict the generic type to a protocol without the use of AnyProtocol ?
Joao
Sent from my iPhone
> On 11 Feb 2016, at 16:53, Developer <devteam.codafi at gmail.com> wrote:
>
> What are the advantages of using AnyProtocol over a simple `class` constraint? To my mind, If you're already using Any or AnyObject in an interface, it seems comfortable enough with dynamic dispatch that knowing the underlying representation of the thing you've got won't do you much good.
>
> In your specific class, my thinking is a delegate need not be a protocol only if an Object presents just a good a target for a message.
>
> ~Robert Widmann
>
> 2016/02/11 9:08、João Nunes via swift-evolution <swift-evolution at swift.org> のメッセージ:
>
>> Hello,
>>
>>
>>
>> I have been thinking about a case that is missing completely from swift.
>>
>>
>>
>> We have the AnyObject type to represent an Object of any type.
>>
>> I recently run upon a situation where i needed a type to represent any Protocol. A AnyProtocol Type would be useful for my case.
>>
>>
>>
>> Example:
>>
>>
>>
>> I have a generic class that needs to receive a protocol type. The code without the AnyProtocol is the following:
>>
>>
>>
>>
>>
>> public class MulticastDelegate<T> {
>>
>>
>>
>>
>>
>>
>>
>> private var delegates = NSHashTable.weakObjectsHashTable()
>>
>>
>>
>>
>>
>>
>>
>> public init() {}
>>
>>
>>
>>
>>
>>
>>
>> public func addDelegate(delegate: T) {
>>
>>
>>
>> guard delegate is AnyObject else { return }
>>
>>
>>
>> delegates.addObject((delegate as! AnyObject))
>>
>>
>>
>> }
>>
>> }
>>
>>
>>
>>
>>
>>
>>
>> This code needs to “trust” that T is a protocol and also make sure T is an Object.
>>
>>
>>
>>
>>
>> Example using AnyProtocol, if it existed:
>>
>>
>>
>> public class MulticastDelegate<T:AnyProtocol> {
>>
>>
>>
>>
>>
>>
>>
>> private var delegates = NSHashTable.weakObjectsHashTable()
>>
>>
>>
>>
>>
>>
>>
>> public init() {}
>>
>>
>>
>>
>>
>>
>>
>> public func addDelegate(delegate: T) {
>>
>>
>>
>>
>>
>> delegates.addObject(delegate)
>>
>>
>>
>> }
>>
>> }
>>
>>
>>
>> Note that the protocol must be a class protocol. This could also be achieved with a where clause or AnyClassProtocol variant.
>>
>>
>>
>> This example is also available on github: https://github.com/jonasman/MulticastDelegate
>>
>>
>>
>> To extend my proposal, there might be cases where AnyStruct and AnyEnum would be usefull. I have no examples for these cases, but i believe someone might have it.
>>
>>
>>
>> What are your thoughts, should I open a PR proposal?
>>
>>
>>
>>
>>
>> João Nunes
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160217/81867dd5/attachment.html>
More information about the swift-evolution
mailing list