[swift-users] question on generics

Slava Pestov spestov at apple.com
Wed Feb 22 20:15:07 CST 2017


> On Feb 22, 2017, at 4:23 PM, David Sweeris via swift-users <swift-users at swift.org> wrote:
> 
> 
>> On Feb 22, 2017, at 16:08, Dave Reed via swift-users <swift-users at swift.org> wrote:
>> 
>> I suspect this can't be done (at least not right now), but wanted to check.
>> 
>> I'd like to declare a class as a generic that meets a protocol and is also a subclass of some specific type.
>> 
>> Something like class Foo<T:NSManangedObject,SomeProtocol> (i.e., the T must be both a NSManagedObject or subclass of it and conform to SomeProtocol).
>> 
>> Is this possible?
> 
> Yep
> class Foo <T: NSManangedObject> where T:  SomeProtocol {...}
> should work fine (although I'm not in front of my computer to double check)

Also this might be more readable,

class Foo<T> where T : NSManagedObject, T : SomeProtocol { … }

Slava

> 
> - Dave Sweeris 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users



More information about the swift-users mailing list