Generics Swift编译器在where子句上崩溃

Generics Swift编译器在where子句上崩溃,generics,swift,crash,Generics,Swift,Crash,周末我开始和斯威夫特一起玩。我正在尝试构建一个类似计算器的应用程序。因此,我想对表达式树进行建模 整个swift编译器套件/xcode似乎仍然非常不稳定。我每隔几分钟就撞车一次。 这就是为什么我想知道我的代码是错误的,还是只是编译器出了问题 这两个协议都很好 protocol BinaryOperator { typealias LeftType typealias RightType typealias ResultType func apply(left:

周末我开始和斯威夫特一起玩。我正在尝试构建一个类似计算器的应用程序。因此,我想对表达式树进行建模

整个swift编译器套件/xcode似乎仍然非常不稳定。我每隔几分钟就撞车一次。 这就是为什么我想知道我的代码是错误的,还是只是编译器出了问题


这两个协议都很好

protocol BinaryOperator {
    typealias LeftType
    typealias RightType
    typealias ResultType

    func apply(left: LeftType, right: RightType) -> ResultType
}

protocol UnaryOperator {
    typealias SourceType
    typealias ResultType

    func apply(SourceType) -> ResultType
}
但是添加以下类时,编译器会在类的第一行出现分段错误而崩溃。我怀疑这是由where子句引起的,因为如果我删除它,编译器不会再崩溃,但由于类型不匹配,它仍然无法编译

class BinaryExpression<O:BinaryOperator, L:Expression, R:Expression
where L.ResultType==O.LeftType, R.ResultType==O.RightType> : Expression {

    typealias ResultType = O.ResultType

    let op : O
    let lhs : L
    let rhs : R

    init(op o : O, left : L, right: R) {
        op = o
        lhs = left
        rhs = right
    }

    func eval() -> ResultType {
        let left : O.LeftType = lhs.eval()
        let right : O.RightType = rhs.eval()

        return op.apply(left, right: right)
    }
}
类二进制表达式:表达式{
typealias ResultType=O.ResultType
让我们开始吧
让lhs:L
让rhs:R
初始(操作o:o,左:L,右:R){
op=o
左=左
右=右
}
func eval()->ResultType{
left:O.LeftType=lhs.eval()
let right:O.RightType=rhs.eval()
返回操作应用(左、右:右)
}
}
我找到了解决方案。。。 问题是where子句中相等运算符处的类型顺序

比较(崩溃)

类二进制表达式
{}
与(工作)

类二进制表达式
{}
class BinaryExpression
<O:BinaryOperator, L:Expression, R:Expression 
where L.ResultType==O.LeftType, R.ResultType==O.RightType> {}
class BinaryExpression
<O:BinaryOperator, L:Expression, R:Expression 
where O.LeftType==L.ResultType, O.RightType==R.ResultType> {}