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

Andrew Bennett cacoyi at gmail.com
Tue Dec 8 04:31:33 CST 2015


I've wanted similar too. Although it may be better, instead of Bool, to
have it as an optional tuple, the reason I suggest this is so that you can
get the associated type. For example:

case let .Link(value,link) = self

So your example could be written:

func isEmpty() -> Bool {
    return (case .Empty = self) != nil
}

However I'm not sure about this syntax, perhaps something like this:

func isEmpty() -> Bool {
    return self is case .Empty
}

Alternatively if you want the value you could do:

let (value, link) = self as? case .Link


On Tue, Dec 8, 2015 at 3: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
>      }
> }
>
> It gets rid of some boilerplate (explicit return true and return false),
> and seems intuitive that it would work given that you can already do
>
> if case .Empty = self {
>      return true
> } else {
>      return false
> }
>
> I haven't really thought through all the implications here, so there's
> probably something I'm missing. It also may be useful only in such limited
> situations that it's not worth the trouble to add.
>
> -Alex
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151208/6a63948e/attachment.html>


More information about the swift-evolution mailing list