How to correctly do line continuation in Julia?

Viewed 579

In fortran, we can simply use & to do line continuation.

But I wonder, in Julia, is there a safe way to do line continuation?

I heard that sometimes Julia cannot identify line continuation and it can cause bugs? Because after, Julia does not seem to have any symbol to do line continuation. Can Julia correctly recognize the line continuation?

Like I define the below function with long arguments,

function mean_covar_init(kmix::Int64,dim_p::Int64,weight::Array{Float64,1},sigma::Array{Float64,2},mu::Array{Float64,2})
return nothing
end

If I do things like

function mean_covar_init(kmix::Int64
,dim_p::Int64
,weight::Array{Float64,1}
,sigma::Array{Float64,2}
,mu::Array{Float64,2})
return nothing
end

Is it safe? Thank you very much!

1 Answers

If you do the thing like you have presented it is safe because Julia sees ( in the first line of code so it will look for a closing ).

However a problematic code would be:

f() = 1
    + 2

The reason is that the f() = 1 part is a valid and complete function definition. Therefore you need to make sure to signal Julia that the line is incomplete. The three most typical ways to do it are:

  1. Move the + to the end of first line:
f() = 1 +
    2
  1. Use ( and ) as a wrapper:
f() = (1
    + 2)
  1. Use begin and end:
f() = begin 1
    + 2 end

Let me give another example with macros, which do not require parenthesis or punctuation and therefore can be often tricky. Therefore the following:

@assert isodd(4) "What even are numbers?"

if rewritten as

@assert isodd(4)
    "What even are numbers?"

does not produce what you expect, and you need to do e.g.:

@assert(isodd(4),
    "What even are numbers?")
Related