Line breaks in Julia

So most of the time, I do not need to worry about line breaks like in Matlab (...)?

Below is an example of a Julia code that can extend to many lines (I have many A, B, C,… and each of them are very long):

IndexT2 = map(A, B, C, D, E) do a, b, c, d, e
(a >= -20) && (b >= 15) && (c > 0) && (d == 2) && (e >= 500);
end

Unfortunately, if I break it like the below, Julia will report errors and refuse to execute:

IndexT2 = map(A, B, C, D, E) 
do a, b, c, d, e
(a >= -20) && (b >= 15) && (c > 0) && (d == 2) && (e >= 500);
end

In this case, does Julia want a line break?

No, Julia does not want a line break, it will throw an error.

FWIW this is an actual 2.0 feature I would like to see. Maybe it’s possible before then but I would love a syntax to split things like this onto separate lines.

1 Like

Unfortunately this is no MWE, so I am not sure wether this works in this case. Did you try enclosing the lines in parentheses?

An MWE

julia> x = [1]; y = [2];

julia> map(x, y) 
       do xx, yy
           xx + yy
       end
ERROR: syntax: invalid "do" syntax
Stacktrace:
 [1] top-level scope
   @ none:1

But it is true that wrapping everything in Parentheses works (which is a bit unintuitive)

julia> (map(x, y) 
       do xx, yy
           xx + yy
       end)
1-element Vector{Int64}:
 3
4 Likes

On Julia version 1.6, as long as you’re careful to use parentheses when using &, you could use the following multi-line syntax (instead of using map):

@. (A > 1) &
   (B > 2) &
   (C > 3)

To make my example runnable, here are some values for the variables:

A = 1:2
B = 2:3
C = 3:4
1 Like

https://docs.julialang.org/en/v1/manual/noteworthy-differences/

In Julia, ... is not used to continue lines of code. Instead, incomplete expressions automatically continue onto the next line.

So also all other incomplete expression will work

x = 1+
2
3 Likes