Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LRUCache does not compile with newer Swift #964

Open
rainbowcardiod opened this issue Feb 22, 2021 · 1 comment
Open

LRUCache does not compile with newer Swift #964

rainbowcardiod opened this issue Feb 22, 2021 · 1 comment

Comments

@rainbowcardiod
Copy link

The compiler is complaining about type mismatches, priority.first is returning the element of the node, while key2node is expecting the node:
Cannot assign value of type 'KeyType?' to subscript of type 'LinkedList<KeyType>.LinkedListNode<KeyType>?'
at line [1]

My solution is to change key2node[key] = first of insert() [1]
with key2node[key] = priority.node(at: 0).

@yuvashrikarunakaran
Copy link

// Before change (which caused the error)
key2node[key] = priority.first

// After the change
key2node[key] = priority.node(at: 0)

swift
class LinkedList {
class LinkedListNode {
var value: KeyType
var next: LinkedListNode?

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

var head: LinkedListNode<KeyType>?

// Example method to get a node at a given index
func node(at index: Int) -> LinkedListNode<KeyType>? {
    var current = head
    var currentIndex = 0

    while current != nil {
        if currentIndex == index {
            return current
        }
        current = current?.next
        currentIndex += 1
    }

    return nil // Return nil if index is out of bounds
}

// Other methods for LinkedList...

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants