在Stack Overflow上的另一个问题中,答案包括以下函数:
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)
Run Code Online (Sandbox Code Playgroud)
它被解析没有错误.where为了便于阅读,在添加新行前句时,突然出现错误:
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 {"
Run Code Online (Sandbox Code Playgroud)
最后,当参数列表括号移动到该where行时,错误再次消失:
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)
Run Code Online (Sandbox Code Playgroud)
这种语法背后的逻辑是什么,应该修复吗?
这类似于该语言中的许多其他语法; 如果解析器在一行的末尾有一个"完整"语法,它将使用它并继续前进.
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
Run Code Online (Sandbox Code Playgroud)
请注意,这意味着您仍然可以将where子句分解为单独的行,但是它where本身需要与函数末尾位于同一行.