符合Swift中Comparable的泛型类

时间:2022-11-13 16:09:08

I'm attempting to create a simple generic node class that conforms to the Comparable protocol so that I can easily compare nodes without accessing their key. When I attempt to write the < and == functions, however, the compiler doesn't seem to like it. The < and == functions expect a type when defining the Node parameters. This was simple in Java where you defined equality and < internally to the class. Swift asks for it globally. Any thoughts ?

我正在尝试创建一个符合Comparable协议的简单通用节点类,以便我可以轻松地比较节点而无需访问其密钥。但是,当我尝试编写 <和==函数时,编译器似乎并不喜欢它。 <和="=函数在定义Node参数时需要一种类型。这在Java中很简单,您可以在其中定义相等性和<内部类。斯威夫特在全球范围内要求它。有什么想法吗" ?< p>

Example:

func < (lhs:Node<E:Comparable>, rhs:Node<E:Comparable>) -> Bool {
    return lhs.key < rhs.key
}


func == (lhs:Node<E:Comparable>, rhs:Node<E:Comparable>) -> Bool {
    return lhs.key == rhs.key
}


class Node<D:Comparable>: Comparable {

    var key: D!
    var next:Node?
    var prev:Node?

    init( key:D ) {

        self.key = key
    }
}

2 个解决方案

#1


8  

You're close! The Node class already specifies that for Node<D>, D must conform to Comparable. Therefore, Node<E: Comparable> in the decl for == and < is redundant. Instead, you want to restrict the types that the operators can be invoked upon:

你很亲密! Node类已经指定对于Node ,D必须符合Comparable。因此,==和 <的decl中的node 是多余的。相反,您希望限制可以调用运算符的类型:

func < <E: Comparable>(lhs: Node<E>, rhs: Node<E>) -> Bool {
    return lhs.key < rhs.key
}


func == <E: Comparable>(lhs: Node<E>, rhs: Node<E>) -> Bool {
    return lhs.key == rhs.key
}

class Node<D: Comparable>: Comparable {

    var key: D!
    var next: Node?
    var prev: Node?

    init(key: D) {
        self.key = key
    }
}

#2


2  

You were very close. Small syntax issue Try this:

你非常接近。小语法问题试试这个:

class Node<D:Comparable>: Comparable {

    var key: D!
    var next:Node?
    var prev:Node?

    init( key:D ) {

        self.key = key
    }

}

func < <E:Comparable> (lhs:Node<E>, rhs:Node<E>) -> Bool {
    return lhs.key < rhs.key
}

func == <E:Comparable> (lhs:Node<E>, rhs:Node<E>) -> Bool {
    return lhs.key == rhs.key
}

#1


8  

You're close! The Node class already specifies that for Node<D>, D must conform to Comparable. Therefore, Node<E: Comparable> in the decl for == and < is redundant. Instead, you want to restrict the types that the operators can be invoked upon:

你很亲密! Node类已经指定对于Node ,D必须符合Comparable。因此,==和 <的decl中的node 是多余的。相反,您希望限制可以调用运算符的类型:

func < <E: Comparable>(lhs: Node<E>, rhs: Node<E>) -> Bool {
    return lhs.key < rhs.key
}


func == <E: Comparable>(lhs: Node<E>, rhs: Node<E>) -> Bool {
    return lhs.key == rhs.key
}

class Node<D: Comparable>: Comparable {

    var key: D!
    var next: Node?
    var prev: Node?

    init(key: D) {
        self.key = key
    }
}

#2


2  

You were very close. Small syntax issue Try this:

你非常接近。小语法问题试试这个:

class Node<D:Comparable>: Comparable {

    var key: D!
    var next:Node?
    var prev:Node?

    init( key:D ) {

        self.key = key
    }

}

func < <E:Comparable> (lhs:Node<E>, rhs:Node<E>) -> Bool {
    return lhs.key < rhs.key
}

func == <E:Comparable> (lhs:Node<E>, rhs:Node<E>) -> Bool {
    return lhs.key == rhs.key
}