2

Swift - 具有可交换性的关联值的模式匹配枚举

斯威夫特 5.0,Xcode 10.2.1

Expression在 Swift 中有一个枚举。

enum Expression {
    indirect case add(Expression, Expression)
    indirect case subtract(Expression, Expression)
    indirect case multiply(Expression, Expression)
    indirect case divide(Expression, Expression)
    indirect case power(Expression, Expression)
    indirect case log(Expression, Expression)
    indirect case root(Expression, Expression)
    case x
    case n(Int)
}

extension Expression: Equatable {
    static func == (lhs: Expression, rhs: Expression) -> Bool {
        switch (lhs, rhs) {
        case let (.add(a, b), .add(c, d)) where a == c && b == d,
             let (.subtract(a, b), .subtract(c, d)) where a == c && b == d,
             let (.multiply(a, b), .multiply(c, d)) where a == c && b == d,
             let (.divide(a, b), .divide(c, d)) where a == c && b == d,
             let (.power(a, b), .power(c, d)) where a == c && b == d,
             let (.log(a, b), .log(c, d)) where a == c && b == d,
             let (.root(a, b), .root(c, d)) where a == c && b == d:
            return true
        case let (.n(a), .n(b)) where a == b:
            return true
        case (.x, .x):
            return true
        default:
            return false
        }
    }
}

第一次尝试

我对我的Expression类型执行了很多模式匹配。加法和乘法的交换性使模式匹配表达式变得很长。我想找到一种方法来简化和缩短它,所以我决定创建一个ExpressionPattern枚举并定义模式匹配运算符 ( ~=) 的重载。

enum ExpressionPattern {
    case commutativeMultiply(Expression, Expression)
    case commutativeAdd(Expression, Expression)
}

func ~= (lhs: ExpressionPattern, rhs: Expression) -> Bool {
    switch lhs {
    case let .commutativeMultiply(a, b):
        switch rhs {
        case .multiply(a, b), .multiply(b, a):
            return true
        default:
            return false
        }

    case let .commutativeAdd(a, b):
        switch rhs {
        case .add(a, b), .add(b, a):
            return true
        default:
            return false
        }
    default:
        return false
    }
}

我希望能够替换模式匹配语句,例如:

case let .add(.n(3), a), let .add(a, .n(3)) where a > 10: //matches (a + 3), (3 + a)
//...

和:

case let .commutativeAdd(.n(3), a) where a > 10: //matches (a + 3), (3 + a)
//...

不过,当我第一次尝试这样做时,我收到一条错误消息,说“模式变量绑定不能出现在表达式中”。

注意:如果我使用没有值绑定的准确的最终值,我认为这个匹配有效,但我在整个项目的许多地方都使用了这个特性。

尝试用法:

let expression: Expression = Expression.add(.divide(.n(1), .n(2)), .subtract(.n(3), .n(4)))
switch expression {

case let .commutativeAdd(.subtract(a, b), .divide(c, d)): //Error: Pattern variable binding cannot appear in an expression.
    print("This matches: ((\(a) - \(b)) + (\(c) ÷ \(d))) and ((\(c) ÷ \(d) + (\(a) - \(b)))")

default: 
   break

}


第二次尝试

对于我的第二次尝试,我更改了Expression'==函数的定义,并尝试覆盖其默认实现~=.

extension Expression: Equatable {
    static func == (lhs: Expression, rhs: Expression) -> Bool {
        switch (lhs, rhs) {
        case let (.add(a, b), .add(c, d)) where  (a == c && b == d) || (a == d && b == c),
             let (.subtract(a, b), .subtract(c, d)) where a == c && b == d,
             let (.multiply(a, b), .multiply(c, d)) where  (a == c && b == d) || (a == d && b == c),
             let (.divide(a, b), .divide(c, d)) where a == c && b == d,
             let (.power(a, b), .power(c, d)) where a == c && b == d,
             let (.log(a, b), .log(c, d)) where a == c && b == d,
             let (.root(a, b), .root(c, d)) where a == c && b == d:
            return true
        case let (.n(a), .n(b)) where a == b:
            return true
        case (.x, .x):
            return true
        default:
            return false
        }
    }

    static func ~= (lhs: Expression, rhs: Expression) -> Bool {
        return lhs == rhs
    }
}

尝试用法:

let expression: Expression = Expression.add(.divide(.n(1), .n(2)), .subtract(.n(3), .n(4)))
print(expression == .add(.subtract(.n(3), .n(4)), .divide(.n(1), .n(2)))) //Prints "true"

switch expression {
case .add(.subtract(.n(3), .n(4)), .divide(.n(1), .n(2))):
    print("Matched")
default:
    print("Not matched")
}

//Prints "Not matched"

注意:理想情况下,这应该打印“匹配”。


问题

在考虑某些情况的交换性的同时,我如何找到一种使用成熟模式匹配的方法。

注意:这必须能够用于匹配具有可交换性的嵌套案例(即.add(.multiply(..., ...), ...))。这一点尤其重要,因为在匹配加法和乘法时需要更多的情况。

4

1 回答 1

-1

我改变了你的第二种用法如下

switch expression {
case let e where e == .add(.subtract(.n(3), .n(4)), .divide(.n(1), .n(2))):
    print("Matched")
default:
    print("Not matched")
}

我认为它会起作用。

于 2019-06-12T07:57:48.603 回答