Does Swift have anything similar to Haskell's as-patterns used in pattern matching? I'm trying to get rid of the second switch
statement in the following piece of code by using a nested-pattern:
indirect enum Type: CustomStringConvertible {
case Int
case Fun(Type, Type)
var description: String {
switch self {
case .Int: return "int"
case .Fun(let p, let r):
switch p {
case .Fun(_): return "(\(p)) -> \(r)"
case _: return "\(p) -> \(r)"
}
}
}
}
Type.Int // "int"
Type.Fun(.Int, .Int) // "int -> int"
Type.Fun(Type.Fun(.Int, .Int), .Int) // "(int -> int) -> int"
The Haskell equivalent, using as-patterns, would be this:
data Type =
Int
| Fun Type Type
desc :: Type -> String
desc t =
case t of
Int -> "int"
Fun (p @ (Fun _ _)) r -> "(" ++ desc p ++ ") -> " ++ desc r
Fun p r -> desc p ++ " -> " ++ desc r
Not the same as the Haskell as-pattern, but you can get rid of the second switch statement with a nested pattern like this:
var description: String {
switch self {
case .Int: return "int"
case .Fun(.Fun(let p, let q), let r): return "(\(Type.Fun(p, q))) -> \(r)"
case .Fun(let p, let r): return "\(p) -> \(r)"
}
}
or by rearranging the cases:
var description: String {
switch self {
case .Int: return "int"
case .Fun(.Int, let r): return "int -> \(r)"
case .Fun(let p, let r): return "(\(p)) -> \(r)"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With