Why is Julia's where syntax sensitive to newlines?

In another stack overflow question, the answer included the following function:

julia> function nzcols(b::SubArray{T,2,P,Tuple{UnitRange{Int64},UnitRange{Int64}}}) where {T,P<:SparseMatrixCSC} return collect(i+1-start(b.indexes[2]) for i in b.indexes[2] if b.parent.colptr[i]<b.parent.colptr[i+1] && inrange(b.parent.rowval[nzrange(b.parent,i)],b.indexes[1])) end nzcols (generic function with 3 methods) 

And he was analyzed without errors. When adding a new line, an error appeared before the where clause for readability:

 julia> function nzcols(b::SubArray{T,2,P,Tuple{UnitRange{Int64},UnitRange{Int64}}}) where {T,P<:SparseMatrixCSC} return collect(i+1-start(b.indexes[2]) for i in b.indexes[2] if b.parent.colptr[i]<b.parent.colptr[i+1] && inrange(b.parent.rowval[nzrange(b.parent,i)],b.indexes[1])) end ERROR: syntax: space before "{" not allowed in "where {" 

Finally, when the parameter list bracket moves to the where line, the error disappears again:

 julia> function nzcols(b::SubArray{T,2,P,Tuple{UnitRange{Int64},UnitRange{Int64}}} ) where {T,P<:SparseMatrixCSC} return collect(i+1-start(b.indexes[2]) for i in b.indexes[2] if b.parent.colptr[i]<b.parent.colptr[i+1] && inrange(b.parent.rowval[nzrange(b.parent,i)],b.indexes[1])) end nzcols (generic function with 3 methods) 

What is the logic of this syntax and should it be fixed?

+7
julia-lang
source share
1 answer

This is similar to many other syntaxes in the language; if the parser has “full” syntax at the end of the line, it will use it and move on.

 julia> parse("begin; 1 \n+ 2; end") quote # none, line 1: 1 # none, line 2: +2 end julia> parse("begin; 1 +\n 2; end") quote # none, line 1: 1 + 2 end 

Please note that this means that you can still split the where clause into a separate line, but where itself must be on the same line as the end of the function.

+9
source share

All Articles