相关疑难解决方法(0)

为什么Julia中的`where`语法对新行敏感?

在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

6
推荐指数
1
解决办法
370
查看次数

标签 统计

julia ×1