Passing values to function arguments using Dict?

Hi,
I have a problem where a function already defined in Julia has several keyword arguments. Now, to pass in values to these arguments, I’ve got to do something like the following

dummyFn(3, 4; key1=6, key2=9, key3=6, key4=5)

I’d like to be able to pass values to these keyword arguments either as a tuple or as a dict. For example, I can create a dictionary named vals and pass the dictionary to the dummyFn without necessarily referring to the keyword arguments

vals = Dict(:key1 => 6, :key2=> 9, :key3 => 6, :key4 => 5)

dummyFn(3, 4, vals)

This way, I can plug in the dictionary everytime I call the dummyFn in my code without needing to referring to the keyword arguments. Could this be possible in Julia?

Yes, it is possible with the “splat” operator: dummyFn(3, 4; vals...) will do the trick.
Note, however, that using a named tuple for vals will be more performant if you don’t need to change the values associated with the keys:

vals = (key1 = 6, key2= 9, key3 = 6, key4 = 5)
dummyFn(3, 4; vals...)
2 Likes

Thanks Vasily, but since I still work with Juliav0.6.3, creating a named tuple throws up an error. Is there a workaround? I tried using the splat operator in comination with a dictionary and it worked fine! Thanks again.

NamedTuples.jl, I guess.

Is there something in particular that holding you back? You’re missing out on a lot of great stuff, and that version is technically no longer supported. I understand upgrading can be hard sometimes, and pre-1.0 I remember it being hard to keep up, but things have been quite stable for the last 2 years, and only getting better

3 Likes

Not really and I’d love to make the switch. As a part of this project I’m working on, I’ve continued using v0.6.3 from the beginning and since its nearing completion, I didn’t want to mess up the existing (lots of) code by upgrading to the newest version.