Skip to content
Advertisement

Julia: Passing Dict items as arguments to a function

I am trying to pass a Dict to a function in Julia. The Dict contains pairs of argument names and their respective values. Let’s say I have a function f and a Dict d:

JavaScript

This throws a MethodError:

JavaScript

I also tried using a NamedTuple, but this seems useless as it is sensitive to the order of the elements in the tuple and doesn’t match their names with the function argument names:

JavaScript

I am sure there is a way to do this. Am trying to do this with the wrong types? In python, what I am looking for would look like this:

JavaScript

Advertisement

Answer

Firstly, since you want the names of the arguments to be important (keyword arguments) you need to use a ; in the function definition:

JavaScript

Secondly, you should use a dictionary where the keys are Symbols:

JavaScript

And thirdly, you want to indicate that you are splatting keyword arguments by splatting after ;:

JavaScript
Advertisement