?" in lin...">

Can't assign a value of type Node <T> to input Node <_>?

Xcode complains that it "cannot assign a value of type Node to input Node <_>?" in lines 23, 24, 26, 27 (assignments of "node" "above" and "bottom" in the conditional part of the queue). I'm not sure what this means and why Xcode sees the difference in types Node and top / bottom

class Node<T> {
    var key: T?
    weak var next: Node<T>?
    weak var previous: Node<T>?

    init(key: T, previous: Node? = nil) {
        self.key = key
        self.previous = previous
    }
}


class Dequeue<T> {
    private var count: Int = 0
    private weak var top: Node<T>?
    private weak var bottom: Node<T>?

    func enqueue<T>(val: T) {
       // if dequeue is empty
       let node = Node<T>(key: val)

        if top == nil {
            self.top = node
            self.bottom = node
        } else {
            self.bottom?.next = node
            self.bottom = node
        }
     }

}
+4
source share
1 answer

Remove the generic <T>from the method declaration.

class Dequeue<T> {
    ...

    func enqueue(val: T) {
        ...
     }    
}

<T> , . <T> T, T; T, .

+6

Source: https://habr.com/ru/post/1617687/


All Articles