【问题标题】:Swift compiler crashes on where clauseSwift 编译器在 where 子句上崩溃
【发布时间】:2014-11-10 14:46:39
【问题描述】:

周末我开始玩 Swift。我正在尝试构建一个类似应用程序的计算器。因此,我想建模一棵表达式树。

整个 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)
    }
}

【问题讨论】:

    标签: generics swift crash


    【解决方案1】:

    我找到了解决方案... 问题在于 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> {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多