function - Julia: Same function for multiple types?
问题描述
I have this big function that I defined on a vector, but I'd like it to work also with a single value. I'd like the type of the first argument to be either a vector or a number.
I triend the following:
function bigfunction(x::Vector, y::Float64=0.5)
# lots of stuff
z = x .+ y
return z
end
bigfunction(x::Number) = bigfunction()
The function works on a vector, but not on the number.
bigfunction([0, 1, 3])
bigfunction(2)
Should I do something with Union{}
as I've seen sometimes? Or redefining the method in a different way?
解决方案
这个问题和回答帮助我说明了Chris Rackauckas 在关于 Julia 类型调度的精彩博客文章中提出的观点。
我已将响应整理到以下代码中:
# 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
推荐阅读
- java - java.lang.ClassNotFoundException: org.apache.commons.collections.ListUtils 当代码在服务器上执行时
- c# - 使用快捷键最小化和恢复窗口
- google-sheets - 识别具有重复条目的行并添加列值
- python - 如何使用python替换qml线系列中的多个点?
- android - Android:什么是 InputConnectionWrapper,它有什么作用?
- php - 文件正在使用 PHP 写入错误的路径
- reactjs - 如何在 React 组件、JSX 中嵌入 Eventbrite iframe/脚本
- firebase-realtime-database - DASH 流媒体 ExoPlayer android 工作室
- animation - 如何在 xamarin 表单中进行页面卷曲翻转
- github - 如何用代理配置github?