One-line `if ... then ...` syntax

You can use parentheses for that

julia> true && (x=nothing) # nothing

though I’m not sure how that’s

  Short-circuiting boolean AND.
3 Likes

Is this a somewhat contrived example designed only to demonstrate issues with &&? Because it seems like a Python-ism.

In Julia, you would do

function bar(x::AbstractVector, m::Int=length(x))
   ... 
end

(Well, actually you would avoid the slice, etc, by writing two entirely separate methods.)

4 Likes

Whoa, since when does this work? (Since forever, probably.) You’re right: This is a leftover Python habit.

2 Likes

Yes, basically forever😄 It’s implemented via multiple dispatch, so you will see that it defines two separate methods.

4 Likes

Hi.
i really hope that any IF must have an END, without (start of line) exception.
the required END really helps me with organizing and reading, so i would not like to have this pattern broken with an IF that cant have an END. I wouldnt use it in my code because it would definitely throw me off.
when it comes to && vs then, || vs or, etc. im fine with both, but i prefer typing and reading then/and/or/…
im fine with shortcut versions that dont have IF-END. why not?

1 Like

Comprehensions can have if without end, like this:

[x for x in 1:10 if iseven(x)]

But I think this is still quite clear and easy to read.

hi. thanks for pointing out this different use of if.
im still confident that everyone knows i mean “if” at the start of the line.