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

标签 function arguments julia

问题是关于 Julia 的“最佳实践”。我已阅读 thisthis .我有一个功能

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/

相关文章:

c++ - 使用多个参数调用构造函数

bash - 无法评估函数的脚本参数

dictionary - 在 Julia 中,我如何找出为什么 Dict{String, Any} 是 Any?

php - PHP函数中的mysql函数

在c编程中不能使用send()发送.exe

javascript - 同一个函数参数可以接受不同的数据类型吗?

database - 如何使用用户定义的字符串索引数据框?

julia - 在 Julia 中打印当前时间

javascript - 如何使用 jQuery 替换跨度内的文本

javascript - 使用动态高度调整 div 内的 Div 大小