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

Kevin Lundberg kevin at klundberg.com
Tue Dec 8 08:17:54 CST 2015


Adding additional behavior to is/as that doesn't deal directly with types seems confusing to me. Those operators I feel would be best left for checking and casting types, despite how much nicer this reads.

--
Kevin Lundberg

> On Dec 8, 2015, at 5:31 AM, Andrew Bennett via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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
> 
> 
> _______________________________________________
> 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/1348adc6/attachment.html>


More information about the swift-evolution mailing list