[swift-evolution] Allowing non-binding pattern matching as a Bool expression?

Chris Lattner clattner at apple.com
Tue Dec 8 23:45:18 CST 2015


> On Dec 7, 2015, at 8:05 PM, Alex Lew via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hi all,
> 
> Curious to hear thoughts on allowing non-binding pattern matches to be used as boolean values outside of an if, guard, for...in, while, switch, etc. Something like:
> 
> enum List<T> {
>      case Empty
>      indirect case Link(T, List<T>)
>      
>      func isEmpty() -> Bool {
>           return case .Empty = self
>      }
> }

I agree with you that this is a problem that we should solve, but I think it could be solved in a different way.  Imagine if:

enum Foo {
  case X(a : Float), Y, Z(a : Int)
}

automatically synthesized these members (the exact names are just a strawman proposal, not serious :-)

extension Foo {
  func isX() -> Float? {…}
  func isY() -> Bool {…}
  func isZ() -> Int? {…}
}

This would tie into all of the mechanics we have for dealing with optionals, e.g. if/let and ??

-Chris


More information about the swift-evolution mailing list