question on generics


(Dave Reed) #1

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?

Thanks,
Dave Reed


(Kevin Nattinger) #2

This seems to work in a playground:

import CoreData

protocol SomeProtocol {}
class Foo<T: NSManagedObject> where T: SomeProtocol {}

···

On Feb 22, 2017, at 4:08 PM, Dave Reed via swift-users <swift-users@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?

Thanks,
Dave Reed

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(David Sweeris) #3

Yep
class Foo <T: NSManangedObject> where T: SomeProtocol {...}
should work fine (although I'm not in front of my computer to double check)

- Dave Sweeris

···

On Feb 22, 2017, at 16:08, Dave Reed via swift-users <swift-users@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?


(Slava Pestov) #4

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

···

On Feb 22, 2017, at 4:23 PM, David Sweeris via swift-users <swift-users@swift.org> wrote:

On Feb 22, 2017, at 16:08, Dave Reed via swift-users <swift-users@swift.org> wrote:

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


(Dave Reed) #5

Cool thanks Kevin and David - I'll give it a try.

···

On Feb 22, 2017, at 7:20 PM, Kevin Nattinger <swift@nattinger.net> wrote:

This seems to work in a playground:

import CoreData

protocol SomeProtocol {}
class Foo<T: NSManagedObject> where T: SomeProtocol {}

On Feb 22, 2017, at 4:08 PM, Dave Reed via swift-users <swift-users@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?

Thanks,
Dave Reed

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users