[swift-evolution] [DRAFT] Regularizing Where Grammar (was Re: Add a while clause to for loops)
Rob Norback
rnorback at gmail.com
Fri Jun 10 14:31:14 CDT 2016
Ok so they are aligned. My mistake. Didn't compile the code first. I got
confused by your statement:
enum X { case a, b }
switch X.a {
case .a, .b where 1 == 0:
print("Matched")
default:
print("Didn't")
}
The `where` clause only applies to the `.b`, not the `.a`, so the code will
print "Matched". To make it print "Didn't", you'd have to write `case .a
where 1 == 0, .b where 1 == 0`
------
Your example was of where used as a break condition and I was applying the
same pattern to the for-in where which is a filter like case-let.
I guess this just illustrates the confusion of the where clauses. Think
I'll just use guards for now.
On Fri, Jun 10, 2016 at 12:15 PM Brent Royal-Gordon <brent at architechies.com>
wrote:
> > Following Brent's logic that the for-in where should mimic the switch
> statement functionality, then this example:
> >
> > for (eachKey, eachValue)
> > where eachValue > 5
> > in theKeyValuePairs {... }
> >
> > Should read like:
> >
> > for (eachKey, eachValue where eachValue > 5) in theKeyValuePairs {... }
> >
> > And,
> >
> > for (eachKey, eachValue)
> > where eachKey % 2 == 0 && eachValue > 5
> > in theKeyValuePairs {... }
> >
> > Should read like:
> >
> > for (eachKey where eachKey % 2 == 0, eachValue where eachValue > 5)
> > in theKeyValuePairs {... }
>
> I'm not sure why you say that. This compiles:
>
> switch pair {
> case let (key, value) where key % 2 == 0 && value > 5:
> print("yes")
> default:
> print("no")
> }
>
> While this fails to compile:
>
> switch pair {
> case let (key where key % 2, value where value > 5):
> print("yes")
> default:
> print("no")
> }
>
> So where are your multiple, parenthesized `where` clauses coming from?
>
> --
> Brent Royal-Gordon
> Architechies
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160610/9d94ff99/attachment.html>
More information about the swift-evolution
mailing list