This question comes about after I had been using the JuliaFormatter.jl package to format some existing code.
Compare the following 4 functions:
function example()
return
end
function example()
return
if a == 1
b = 2
end
return otherFunction(b)
end
function example()
return a == 1 && otherFunction(4)
end
function example()
return if a == 1
otherFunction(4)
else
false
end
end
In the first example, this is a return statement which returns nothing
.
The second example is a bit harder to read. It depends on how return
is parsed.
This is my guess: (I could not find anything about this in the documentation.)
return
is a single line statement. The statement is implicitly ended by a new line.
If the above is correct, then the second example will return nothing
, everytime. The if
statement which follows is not part of the return
statement.
The third example is tricky to read too. It returns false
if a != 1
, and otherwise it returns the value produced by the function call otherFunction(4)
.
The fourth example is exactly the same as the third. But, an if
statement begins on the same line as the return
statement. That makes it different from the second example, and also different from the third example.
- Now, the
return
statement is not ended until theif
statement is completed by the associatedend
. So in this case, areturn
statement can be multi-line.
It seems to be the same as
return (
if a == 1 otherFunction(4) else false end
)
Is anyone able to clarify the parsing rules relating to return
, and also let me know if anything I have written here is not correct?