Group Group Group Group Group Group Group Group Group

LinkedList conformance for Collection

extension LinkedList: Collection {
    public struct Index: Comparable {
        public var node: Node<Value>?
        
        static public func ==(lhs: Index, rhs: Index) -> Bool {
            switch (lhs.node, rhs.node) {
            case let (left?, right?):
                return left.next === right.next
            case (nil, nil):
                return true
            default:
                return false
            }
        }
        
        static public func <(lhs: Index, rhs: Index) -> Bool {
            guard lhs != rhs else {
                return false
            }
            let nodes = sequence(first: lhs.node) { $0?.next }
            return nodes.contains { $0 === rhs.node }
        }
    }
}

in implementation for ==:

  1. I don’t understand why returning true for both nil condition.
  2. when nil condition occurs? I mean consider this code
var list = LinkedList<String>()
list.append("value1")
list.append("value2")
list.append("value3")

for list[0], list[1] and list[2] we have valid value, after that for list[3] and so on, should not we crash? so when list[n] will be nil?