[swift-evolution] Type information containing multiple additional protocols

Daniel Muhra daniel.muhra at gmail.com
Fri Dec 4 17:07:44 CST 2015


Currently it doesn’t seem possible to define a property or variable of a specific class that also implements one or more protocols.
Take as an example the following definition from Objective C:

NSManagedObject<NSCoding, Task>* someObject;

To my knowledge, currently it is only possible to define multiple protocols only:

var object : protocol<NSCoding, Task>

However, given the first example, sometimes it is not possible to restrict to protocols only. This becomes especially cumbersome, if you would need to cast down to such a construct within a single method and perform several steps which would require all those type information:

func doSomething(object: AnyObject) {
    if let managedObject = object as? NSManagedObject {
        // Do core data related stuff
    }
    
    if let task = object as? Task {
        // Do task related stuff
    }  
}

In this case you now have managedObject and task though you operate on the same object. Also it is not (easily) possible to interleave actions of those two.

My first idea here would be to introduce an Objective C like style of type naming. This also has the benefit, that it is perfectly clear, whether a type name refers to a class or a protocol (without first having to check it).
The downside, of course, is that protocol type variables would become something like Any<MyProtocol>

Any other ideas are welcome, but the initial problem still stands and should be addressed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151205/fb927de9/attachment-0001.html>


More information about the swift-evolution mailing list