简体   繁体   中英

declare generic class with a generic class as a parameter in swift

I wrote these simple generic classes and it worked great:

class LinkedListNode <T> {
    var value: T
    var next: LinkedListNode<T>?
    weak var prev: LinkedListNode<T>?
    init(value: T) {
        self.value = value
        self.next = nil
    }
}

class LinkedList<T> {
    var first: LinkedListNode<T>? = nil
    var last: LinkedListNode<T>? = nil
    var count = 0
    @discardableResult func append(_ value: T) -> LinkedListNode<T> {
        let new = LinkedListNode(value: value)
        new.prev = last
        last?.next = new
        count += 1
        last = new
        if first == nil {
            first = new
        }
        return new
    }
}

And I used it like:

let list = LinkedList<Int>()
list.append(3)
let lastNode = list.append(5)

Now I realized there are some cases when I need to have a customized node: CustomNode<T> , subclass of LinkedListNode<T> . So I would like to be able to pass the class to be used as the node as:

let list = LinkedList<CustomNode<Int>>()
list.append(3)
let customNode = list.append(5)

How can I declare my class to have it like this or something similar?

I have tried the following declaration but weird errors rise. Is this even possible?

class LinkedList<Node<T>: LinkedListNode<T>> { ... 

Update 2019/07/26.

Even with Kamran's approach this method does not compile. I am not sure if this is doable without a protocol. See my comment on Kamran's answer.

func remove(node: LinkedListNode<T>) { // change to `func remove(node: U)`
    node.next?.prev = node.prev
    node.prev?.next = node.next
    if node === first {
        first = first?.next
    }
    if node === last {
        last = last?.prev // Error here: "Cannot assign value of LinkedListNode<T>? to U?"
    }
}

You will need to define a protocol with an associated type :

protocol Node: class {
    associatedtype Value
    var value: Value {get set}
    var next: Self? {get set}
    var prev: Self? {get set}

    init(value: Value)
}

final class BasicNode<Value>: Node {
    var value: Value
    var next: BasicNode<Value>?
    weak var prev: BasicNode<Value>?

    init(value: Value) {
        self.value = value
    }
}

final class CustomNode<Value>: Node {
    // customize however you want
    var value: Value
    var next: BasicNode<Value>?
    weak var prev: BasicNode<Value>?

    init(value: Value) {
        self.value = value
    }
}

class LinkedList<N: Node> {
    var first: N? = nil
    var last: N? = nil
    var count = 0

    @discardableResult
    func append(_ value: N.Value) -> N {
        let new = N(value: value)
        new.prev = last
        last?.next = new
        count += 1
        last = new
        if first == nil {
            first = new
        }
        return new
    }
}

However, this will require using your basic linked list in an annoying way all the time:

let list = LinkedList<BasicNode<Int>>()

Depending on how you need to customize the node, I would consider finding a way to customize the behavior in the LinkList class itself using dependency injection .

The syntax you are trying can be achieved as below,

class LinkedListNode <T> {
    var value: T
    var next: LinkedListNode<T>?
    weak var prev: LinkedListNode<T>?
    required init(value: T) {
        self.value = value
        self.next = nil
    }
}

class GenericCustomNode<T>: LinkedListNode<T> {

    required init(value: T) {
        super.init(value: value)
    }
}

class NonGenericCustomNode: LinkedListNode<Int> {

    required init(value: Int) {
        super.init(value: value)
    }
}

class LinkedList<T, U: LinkedListNode<T>> {
    var first: U? = nil
    var last: U? = nil
    var count = 0
    @discardableResult func append(_ value: T) -> U {
        let new = U(value: value)
        new.prev = last
        last?.next = new
        count += 1
        last = new
        if first == nil {
            first = new
        }
        return new
    }

    func remove(node: U) {
        node.next?.prev = node.prev
        node.prev?.next = node.next
        if node === first {
            first = first?.next as? U
        }
        if node === last {
            last = last?.prev as? U
        }
    }
}

Usage:

let list = LinkedList<Int, LinkedListNode<Int>>()
list.append(5)
print(list.first?.value)

let someCustom = LinkedList<Int, GenericCustomNode<Int>>()
someCustom.append(15)

print(someCustom.first?.value)

let otherCustom = LinkedList<Int, NonGenericCustomNode>()
otherCustom.append(2)
print(otherCustom.first?.value)

Output:

 Optional(5) 
 Optional(15) 
 Optional(2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM