繁体   English   中英

如何在协议扩展中为自定义中缀运算符实现默认方法

[英]How to implement a default method for a custom infix operator in a protocol extension

我正在尝试将自定义比较中缀运算符==^实现为标准相等运算符==的精简版本

我的应用程序主要面向协议,因此我试图在协议扩展中实现默认方法static func ==^ 然而,当我让我的 class 符合协议时,我得到一个Type 'MySourceNode' does not conform to protocol 'SourceNodeType'错误,并且 Xcode 让我添加static func ==^协议 5

我的问题是,如何在协议扩展中正确编写默认实现?

我试图在 SO 上找到答案,但其中大多数都比较老,并且只讨论在协议扩展之外定义的通用方法。 但这在我的情况下似乎不起作用。

这是一个游乐场文件,其中包含我的协议的简化版本。 代码后有一些背景信息。 如果有不清楚的地方,请在评论中告诉我,我会相应地更新我的问题。

import Foundation
import SpriteKit

infix operator ==^: ComparisonPrecedence

protocol SourceNodeType: SKShapeNode {

    var constrainZRotation: SKConstraint! { get set }
    func setConstraints()

    static func ==^ (lhs: SourceNodeType, rhs: SourceNodeType) -> Bool

}

extension SourceNodeType {

    func setConstraints() {
        print("Setting constraints")
    }

    static func ==^ (lhs: SourceNodeType, rhs: SourceNodeType) -> Bool {
        lhs.frame == rhs.frame &&
            lhs.position == rhs.position &&
            lhs.constrainZRotation == rhs.constrainZRotation
    }
}

class MySourceNode: SKShapeNode, SourceNodeType {

    var constrainZRotation: SKConstraint!

}

解释这个自定义中缀运算符背后的原因。 我需要它,因为我经常将 SpriteKit 的 SKShapeNodes 的子类相互比较。 但我不希望 class 中的每个变量(如可访问性 label 或名称)都进行比较,因为我在将节点添加到场景时更改了这些值,这部分是在比较之后。

我意识到将func ==^方法放在协议之外,并且仅使用SourceNodeType协议将其实现为rhslhs类型正是我所需要的!

这个答案解决了我的问题:

https://stackoverflow.com/a/41390111/12764795

也许有人可以确认这是 go 的当前正确方法?

import Foundation
import SpriteKit

infix operator ==^: ComparisonPrecedence

func ==^ (lhs: SourceNodeType, rhs: SourceNodeType) -> Bool {
    lhs.frame == rhs.frame &&
        lhs.position == rhs.position &&
        lhs.constrainZRotation == rhs.constrainZRotation
}

protocol SourceNodeType: SKShapeNode {
    var constrainZRotation: SKConstraint! { get set }
    func setConstraints()
}

extension SourceNodeType {
    func setConstraints() {
        print("Setting constraints")
    }
}

class MySourceNode: SKShapeNode, SourceNodeType {
    var constrainZRotation: SKConstraint!
}

let lhsSourceNode = MySourceNode(circleOfRadius: 10)
var rhsSourceNode = MySourceNode(circleOfRadius: 10)
print(lhsSourceNode ==^ rhsSourceNode) // TRUE

rhsSourceNode.name = "Name used for testing"
print(lhsSourceNode ==^ rhsSourceNode) // TRUE

rhsSourceNode.position.y += 100

print(lhsSourceNode ==^ rhsSourceNode) // FALSE

问题是,您无法满足这种协议要求:

static func ==^ (lhs: SourceNodeType, rhs: SourceNodeType) -> Bool

我在Swift 文档中找不到明确的解释,但是如果您尝试使类型符合该协议,它将无法编译,也不应该编译。

我的问题是,如何在协议扩展中正确编写默认实现?

如果要使用SourceNodeType类型的参数,使用免费的 function 是最直接的方法,但删除操作员要求会使编译器错误静音,即:

protocol SourceNodeType: SKShapeNode {

    var constrainZRotation: SKConstraint! { get set }
    func setConstraints()
}

直到您尝试使用您的运营商:

let node = MySourceNode()
let otherNode = MySourceNode()
node ==^ otherNode // Generic parameter 'Self' could not be inferred

您仍然可以通过声明以下内容来解决此问题:

extension SourceNodeType {
    static func ==^ (lhs: Self, rhs: SourceNodeType) -> Bool {
        lhs.frame == rhs.frame &&
            lhs.position == rhs.position &&
            lhs.constrainZRotation == rhs.constrainZRotation
    }
}

它将按预期工作,但您仍然无法将其声明为协议要求,因为它会触发:

协议“SourceNodeType”只能用作通用约束,因为它具有 Self 或关联的类型要求

由于 rhs 是SourceNodeType类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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