Julia中没有自然默认值的命名参数

vat*_*mut 10 arguments function julia

问题是关于朱莉娅的"最佳实践".我读过这个这个.我有一个功能

function discount_rate(n, fv, pmt, pv; pmt_type = 0)
...
end
Run Code Online (Sandbox Code Playgroud)

现在的问题是我必须像这样调用方法

discount_rate( 10, 10, 10, -10 )
Run Code Online (Sandbox Code Playgroud)

目前尚不清楚这些论点意味着什么 - 即使我忘了.我喜欢做的就是写作

discount_rate( n = 10, fv = 10, pmt = 10, pv = -10 )
Run Code Online (Sandbox Code Playgroud)

这更清楚:更容易阅读和理解.但我无法通过创建这些参数keywords参数或optional参数来定义我的方法,因为它们没有自然默认值.从设计的角度来看,有推荐的解决方法吗?

Iai*_*ing 6

可以做到以下几点:

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
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,伊恩.另外,在Youtube上找到你的Julia视频教程非常有用. (2认同)