问题描述
在 R 中,您可以在函数中使用 NULL 参数,例如myfun = 函数 (a=NULL, b, c)你能在 Julia 中做到这一点吗?我之所以问,是因为我想将变量 a 作为条件:如果 a = NULL 执行此操作,否则执行此操作.我当然可以编写不同的函数,但它们大多会相互重复.我也可以只分配任意数字,但似乎使用 NULL 更清楚.谢谢!.
In R you can use NULL arguments in functions like myfun = function (a=NULL, b, c)can you do this in Julia? I'm asking because I'd like variable a as a condition : if a = NULL do this, else do that. I can just write different functions of course but they would mostly repeat each other. I can also just assign arbitrary numbers but it seems using NULL is more clear. Thanks!.
推荐答案
学习新语言的人们最常见的问题之一是如何编写与他们所来自的语言完全一样的代码.但是测试 NULL 可能不是在 Julia 中实现这一点的最佳方法.大多数情况下,您将为您的函数定义两种方法:
One of the most common questions from people coming to a new language is how to code exactly like in the language they come from. But testing for NULL is not likely to be the best way to implement this in Julia. Mostly you'll define two methods for your function:
function myfun(a, b, c)
...
end
function myfun(b, c)
...
end
具有不同的行为.
这篇关于具有 NULL 参数的 Julia 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!