Skip to content

mini julep: if x then y #16389

@quinnj

Description

@quinnj

In various discussions, it has been suggested to allow syntax like:

if x then y

As a short-form "if" statement and as an alternative to the common:

x && y

syntax 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    julepJulia Enhancement Proposal

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions