问题是关于 Julia 的“最佳实践”。我已阅读 this和 this .我有一个功能
function discount_rate(n, fv, pmt, pv; pmt_type = 0)
...
end
现在的问题是我必须像这样调用方法
discount_rate( 10, 10, 10, -10 )
不清楚这些论点是什么意思——即使我忘记了。我想做的是写
discount_rate( n = 10, fv = 10, pmt = 10, pv = -10 )
这更清楚:更容易阅读和理解。但是我无法通过设置这些参数来定义我的方法
keywords
参数或 optional
参数,因为它们没有 天然 默认值。从设计的角度来看,有没有推荐的方法来解决这个问题?
最佳答案
可以做到以下几点:
function discount_rate(;n=nothing,fv=nothing,pmt=nothing,pv=nothing,pmt_type=0)
if n == nothing || fv == nothing || pmt == nothing || pv == nothing
error("Must provide all arguments")
end
discount_rate(n,fv,pmt,pv,pmt_type=pmt_type)
end
function discount_rate(n, fv, pmt, pv; pmt_type = 0)
#...
end
关于function - Julia 中没有自然默认值的命名参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26447854/