Two Generic Types or One

[I realize this is better done with protocols; but I'm exploring a class-only approach with this question!] Assume I have the following generic type defined:


class Entity<Identifier:Equatable> {
  var identifier : Identifier
  // init, etc
}


Now, following a DAO-ish pattern, I would like to define:


class EntityDAO<Entity:Entity> {
  var entities : [Entity]
  // ...
}


Of course, the above won't compile because the 'Identifier' for 'Entity' is not mentioned. Is there a way to define EntityDAO with a single generic type for Entity?


The other option is, with two generic types:


class EntityDAOToo<I:Equatable, E:Entity<I>> { ... }


But I prefer not to specify 'I'. The use would be


class Person : Entity<String> { ... }
class PersonDao : EntityDao<Person> { ... }

Not entirely sure if this does what you want, but:

protocol Entity {
    typealias Identifier: Equatable
    var identifier: Identifier { get }
}
protocol EntityDAO {
    typealias EntityType: Entity
    var entities : [EntityType] { get }
}
struct Person: Entity {
    typealias Identifier = String
    let identifier: String
}
struct PersonDao: EntityDAO {
    typealias EntityType = Person
    let entities: [Person]
}
extension Person: CustomStringConvertible {
    var description: String { return identifier }
}
func test() {
    let a = Person(identifier: "Marge")
    let b = Person(identifier: "Batman")
    let c = Person(identifier: "Yoda")
    let groupOfPeople = PersonDao(entities: [a, b, c])
    print(groupOfPeople)
}
test()
// (For Swift 1.2 instead of 2.0: Replace CustomStringConvertible with Printable, and print with println.)

Thanks but I forgot to mention. I know how to do this with protocols. I'm interested in knowing if it is possible without using protocol definitions. That is, am I missing something that prevents using only `class`

I don't understand what you mean by "That is, am I missing something that prevents using only 'class'?".

Anyway, are you really sure that the best thing you can do is to do it without using protocols?

Even after watching the WWDC 2015 video Protocol-Oriented Programming in Swift? : )

I already have a working implementation using a protocol `EntityType`; I asked the question to explore a class-only approach. Also, thanks for the 'Protocol-Oriented Programming in Swift' link - I'd still like an answer to my question but am willing to admit it is little more than a curiosity now.

Well, this compiles:

class Entity<Identifier: Equatable> {
    var identifier: Identifier
    init(_ id: Identifier) { self.identifier = id }
}

class EntityDao<Entity: Entity<String>> {
    var entities: [Entity]
    init() {
        entities = []
    }
}

class Person : Entity<String> {
}
class PersonDao : EntityDao<Person> {
}

And it answers your question "Is there a way to define EntityDAO with a single generic type for Entity?" if what you mean by "a single generic type for Entity" is as above. Or perhaps you mean something like the following, which also compiles:

class Entity<Identifier : Equatable> {
    var identifier: Identifier
    init(_ id: Identifier) { self.identifier = id }
}

class EntityDao<T: Equatable> {
    var entities: [Entity<T>]
    init() {
        entities = []
    }
}

class Person : Entity<String> {
}
class PersonDao : EntityDao<String> {
}

But I fail to see the point of any of this.

Two Generic Types or One
 
 
Q