一个 Julia 函数的写法理解,其实这个问题已经在 julia中花括号的作用及用法是? 中提到过了,但是刚看到时没反应过来,所以再贴一次。看下面的代码
struct Node{dim,T}
x::Vec{dim,T}
end
Node(x::NTuple{dim,T}) where {dim,T} = Node(Vec{dim,T}(x))
其中
Node(x::NTuple{dim,T}) where {dim,T} = Node(Vec{dim,T}(x))
是一个函数的紧凑形式,写成完全的形式应该为
function Node(x::NTuple{dim,T}) where {dim,T}
Node(Vec{dim,T}(x))
end
要习惯这种写法,所以最初的一段代码就应该是
struct Node{dim,T}
x::Vec{dim,T}
end
function Node(x::NTuple{dim,T}) where {dim,T}
Node(Vec{dim,T}(x))
end