[swift-evolution] Haskell-like as-patterns in switch statements

IonuČ› G. Stan ionut.g.stan at gmail.com
Sun Apr 23 08:55:47 CDT 2017


Hi all,

I'm unsure if this is the appropriate venue to discuss this, so 
apologies if it's not.

I was wondering if the issue of supporting as-patterns in switch 
statements has been brought up before or not, and what was the outcome 
of that.

By as-patterns I mean the ability to bind sub-patterns to identifiers. 
See this question of mine on StackOverflow for an example: 
http://stackoverflow.com/questions/43566117/haskell-like-as-patterns-in-swift/43567041#43567041

Maybe an equal sign could be used to denote that, so my example above 
would become:

     switch self {
     case .Int: return "int"
     case .Fun(let p = .Fun, let r): return "(\(p)) -> \(r)"
     case .Fun(let p, let r): return "(\(p) -> \(r))"
     }

Note the `let p = .Fun` part.

Alternatively, enhancing the `where` clause of a `case` to support the 
same features as an `if case` expression, would be an acceptable choice, 
too?

     case .Fun(let p, let r) where case .Fun = p:
         return "(\(p)) -> \(r)"

Thanks for reading.

-- 
IonuČ› G. Stan  |  http://igstan.ro  |  http://bucharestfp.ro


More information about the swift-evolution mailing list