-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Description
In various discussions, it has been suggested to allow syntax like:
if x then yAs a short-form "if" statement and as an alternative to the common:
x && ysyntax which leverages the short-circuiting && operator for conditionally executing y (with y often containing other side effects and not necessarily returning a Bool).
The main advantages to this if-then construct being: more legible code, relying less on abusing &&, and formally including an "if" statement form that doesn't require an end keyword.
It occurred to me the other day, that this syntax would also provide a convenient means for implementing #550, which would look like:
A = [if x % 2 == 0 then f(x) for x in 1:10]Relying on the fact that if-then doesn't require an end keyword, which we would probably need in some form anyway even if we went with python-style guards:
A = [f(x) for x in range(10) if x % 2 == 0]To be clear, the Julia guard syntax would essentially be doing a rewrite from:
A = [if x % 2 == 0 then f(x) for x in 1:10]to
A = [Filter(x->x % 2 == 0, f(x) for x in 1:10)]Also as a clarifying note, this would be allowing the guard syntax at the generator level as opposed to just the comprehension level (which matches what python allows as well).