Julia function arguments are pretty flexible... there's not necessarily a
"right way", as it depends on a few things. A few tricks that some people
might not realize:
function f(args...; kw...)
d = Dict(kw)
if haskey(d, :some_kw_arg)
# do something
end
end
function g()
d = Dict()
d[:some_kw_arg] = myvalue
f(; d...)
end
You can get creative with how you use the splat operator... sometimes it
makes unreadable code nice-looking, other times the opposite. Use your
judgement, and try not to do this stuff in tight inner loops...
On Tue, Mar 8, 2016 at 8:39 AM, 'Tim Loderhose' via julia-users <
[email protected]> wrote:
> Hello everyone,
> I started using Julia recently and am very pleased with the many options I
> am given to write code.
> Now I want to translate a toolbox I wrote in MATLAB to Julia (It's about
> MRI reconstruction).
>
> There are often lots of options in my function arguments, something which
> I handled in a quite ugly way in Matlab, for example:
> A function which loads an image takes in a filename and varargin, where
> varargin can be certain counters I need to construct a matrix from binary
> data.
> If no varargin is supplied, the function calls another function which will
> load these counters from a seperate header file.
> If they are supplied, it expects them to be correct and in the right order
> to function.
>
> Now in Julia I have the option to specify all the variables needed in the
> arguments, and give them default values.
> The default, however, depends on the file which I am loading - if the
> extra arguments are not supplied, I would call the other function which
> gives me the defaults.
>
> Does anyone know an elegant way to handle this in Julia?
> I thought of making keyword arguments which I could all set to 0. Then I
> can check that at the beginning of the function and load the defaults if
> necessary. But that seems ugly to me.
>
> I put some example code here:
> https://gist.github.com/timlod/7bb82e4796dd6404ccfa
>
> If this is too vague, I'll try to concretise it more :)
> Thanks,
> Tim
>
>
>