朱莉娅:多种类型的相同功能?
Julia: Same function for multiple types?
我有一个在向量上定义的大函数,但我希望它也能处理单个值。我希望第一个参数的类型是向量或数字。
我尝试了以下内容:
function bigfunction(x::Vector, y::Float64=0.5)
# lots of stuff
z = x .+ y
return z
end
bigfunction(x::Number) = bigfunction()
该函数适用于矢量,但不适用于数字。
bigfunction([0, 1, 3])
bigfunction(2)
我应该像我有时看到的那样用 Union{}
做点什么吗?或者以不同的方式重新定义方法?
还是鸭子型。请记住,函数总是自动专门化,因此选择受限调度根本不会影响性能。
function bigfunction(x, y=0.5)
# lots of stuff
z = x .+ y
return z
end
这将同样高效,但适用于更多类型。参见 this blog post on type-dispatch designs for more information。
这个问题和回答帮助我阐明了 Chris Rackauckas 在 Julia 中的类型调度上 blog post 所提出的观点。
我已将回复整理成以下代码:
# I ran this only in Julia 1.0.0.
## ========== Original function ==========
## function bigfunction(x::Vector, y::Float64=0.5)
## # lots of stuff
## z = x .+ y
## return z
## end
## bigfunction(x::Number) = bigfunction()
## println(bigfunction([0, 1, 3]))
## println(bigfunction(2))
## ---------- Output has ERROR ----------
## [0.5, 1.5, 3.5]
## ERROR: LoadError: MethodError: no method matching bigfunction()
# ========== Answer Suggested by Picaud Vincent in comments ==========
# Note use of Union in function signature.
function bigfunction(x::Union{Vector, Number}, y::Float64=0.5)
# lots of stuff
z = x .+ y
return z
end
println(bigfunction([0, 1, 3]))
println(bigfunction(2))
## ---------- Output Okay ----------
## [0.5, 1.5, 3.5]
## 2.5
# ========== Answer Suggested by Robert Hönig in comments ==========
# Note change in line right after function definition.
function bigfunction(x::Vector, y::Float64=0.5)
# lots of stuff
z = x .+ y
return z
end
bigfunction(x::Number) = bigfunction([x])
println(bigfunction([0, 1, 3]))
println(bigfunction(2))
## ---------- Output Okay ----------
## [0.5, 1.5, 3.5]
## 2.5
# ========== Answer Suggested by Chris Rackauckas ==========
# Note change in function signature using duct typing--no type for x.
function bigfunction(x, y=0.5)
# lots of stuff
z = x .+ y
return z
end
println(bigfunction([0, 1, 3]))
println(bigfunction(2))
## ---------- Output Okay ----------
## [0.5, 1.5, 3.5]
## 2.5
我有一个在向量上定义的大函数,但我希望它也能处理单个值。我希望第一个参数的类型是向量或数字。
我尝试了以下内容:
function bigfunction(x::Vector, y::Float64=0.5)
# lots of stuff
z = x .+ y
return z
end
bigfunction(x::Number) = bigfunction()
该函数适用于矢量,但不适用于数字。
bigfunction([0, 1, 3])
bigfunction(2)
我应该像我有时看到的那样用 Union{}
做点什么吗?或者以不同的方式重新定义方法?
还是鸭子型。请记住,函数总是自动专门化,因此选择受限调度根本不会影响性能。
function bigfunction(x, y=0.5)
# lots of stuff
z = x .+ y
return z
end
这将同样高效,但适用于更多类型。参见 this blog post on type-dispatch designs for more information。
这个问题和回答帮助我阐明了 Chris Rackauckas 在 Julia 中的类型调度上 blog post 所提出的观点。
我已将回复整理成以下代码:
# I ran this only in Julia 1.0.0.
## ========== Original function ==========
## function bigfunction(x::Vector, y::Float64=0.5)
## # lots of stuff
## z = x .+ y
## return z
## end
## bigfunction(x::Number) = bigfunction()
## println(bigfunction([0, 1, 3]))
## println(bigfunction(2))
## ---------- Output has ERROR ----------
## [0.5, 1.5, 3.5]
## ERROR: LoadError: MethodError: no method matching bigfunction()
# ========== Answer Suggested by Picaud Vincent in comments ==========
# Note use of Union in function signature.
function bigfunction(x::Union{Vector, Number}, y::Float64=0.5)
# lots of stuff
z = x .+ y
return z
end
println(bigfunction([0, 1, 3]))
println(bigfunction(2))
## ---------- Output Okay ----------
## [0.5, 1.5, 3.5]
## 2.5
# ========== Answer Suggested by Robert Hönig in comments ==========
# Note change in line right after function definition.
function bigfunction(x::Vector, y::Float64=0.5)
# lots of stuff
z = x .+ y
return z
end
bigfunction(x::Number) = bigfunction([x])
println(bigfunction([0, 1, 3]))
println(bigfunction(2))
## ---------- Output Okay ----------
## [0.5, 1.5, 3.5]
## 2.5
# ========== Answer Suggested by Chris Rackauckas ==========
# Note change in function signature using duct typing--no type for x.
function bigfunction(x, y=0.5)
# lots of stuff
z = x .+ y
return z
end
println(bigfunction([0, 1, 3]))
println(bigfunction(2))
## ---------- Output Okay ----------
## [0.5, 1.5, 3.5]
## 2.5