use dictionary as named arguments for function

753 views
Skip to first unread message

Westley Hennigh

unread,
Aug 5, 2013, 7:17:03 PM8/5/13
to julia...@googlegroups.com
I hope I'm being dense and that this already exists.

I have some pre-named-args julia which -effectively- flattens a dictionary of named things into an ordered tuple of args that are passed into a function. I'd really like to just use the dictionary as a set of named args. Is that possible?

Mike Nolta

unread,
Aug 5, 2013, 7:53:55 PM8/5/13
to julia...@googlegroups.com
```
julia> f(;kw...) = for (k,v) in kw; println(k,",",v); end
# methods for generic function f
f() at none:1

julia> d = {:a => 1, :b => "c"}
{:b=>"c",:a=>1}

julia> f(;d...)
b,c
a,1
```

-Mike

Westley Hennigh

unread,
Aug 5, 2013, 8:02:07 PM8/5/13
to julia...@googlegroups.com
Heh, ya, but I can always pass a dictionary. It would be sweet if the names could be broken out so that you can read the defaults and we don't end up with big blocks of `get(args, thing, default)` at the top of functions.

Mike Nolta

unread,
Aug 5, 2013, 8:08:45 PM8/5/13
to julia...@googlegroups.com
```
julia> f(;x=0,y=0) = (x,y)
# methods for generic function f
f() at none:1

julia> d = {:x => 3, :y => 4}
{:y=>4,:x=>3}

julia> f(;d...)
(3,4)
```

-Mike

On Mon, Aug 5, 2013 at 8:02 PM, Westley Hennigh

Westley Hennigh

unread,
Aug 5, 2013, 8:14:02 PM8/5/13
to julia...@googlegroups.com
Lol. I apologize for taking your time, thanks!

Westley Hennigh

unread,
Aug 5, 2013, 9:01:56 PM8/5/13
to julia...@googlegroups.com
Ok, I have -perhaps- a slightly more interesting question based on this.

In your above example, suppose you have a dict `d = {"x" => 3, "y" => 4}`. Note the strings.
If you called your function f with said dict, I think (could be wrong) that what you're after is pretty unambiguous (to treat the strings as symbols). What's more, I think that the conversion between strings and symbols can be pretty cheap.

So maybe we could add support for more efficiently calling functions with dictionaries of strings of named args (without generating one containing symbols).

I see line 429 in https://github.com/JuliaLang/julia/blob/master/src/julia-syntax.scm, I think dictionaries are being expanded around like 1670 and then passed as an unsorted list. So maybe this wouldn't be more efficient... Not sure, are you more familiar with this code?

Westley Hennigh

unread,
Aug 5, 2013, 9:04:42 PM8/5/13
to julia...@googlegroups.com
I'm wrong about the purpose of the code at 1670

Jacob Quinn

unread,
Aug 5, 2013, 9:08:12 PM8/5/13
to julia...@googlegroups.com
Auto-converting strings to symbols in this case seems pretty auto-magical (in a bad way). I would just expect the user to call the symbol(x) call themselves before passing them in.

-Jacob

Westley Hennigh

unread,
Aug 5, 2013, 9:38:40 PM8/5/13
to julia...@googlegroups.com
Hmm, maybe it is. I'm a partial judge.

But just to be clear: I don't want to actually modify the dict or pursue this if it will be considerably slower.

I do think it's fairly unambiguous. And if you pulled a dictionary out of some json and wanted to use it as parameters, I think this would seem pretty straightforward / convenient.

Elliot Saba

unread,
Aug 6, 2013, 1:40:50 AM8/6/13
to julia...@googlegroups.com
One of the problems with this is that strings have quite a bit more leeway in what they can contain than symbols.  What happens if your strings have spaces?  Or minus symbols?  Getting an error from the `symbol()` function while trying to splat a dictionary seems like it might be confusing.

If doing this conversion automatically wouldn't take too much cpu time, then doing it manually won't take too much cpu time either.  :)  It should be pretty straightfoward to write e.g. a `dictsplat()` function that converts any string keys to symbols, and throws a meaningful error if they are unable to do so.

Westley Hennigh

unread,
Aug 6, 2013, 11:39:25 AM8/6/13
to julia...@googlegroups.com
The problem with error handling hadn't occurred to me - This was a bad idea.

Thanks all! Sorry for the noise.

Ivar Nesje

unread,
Aug 7, 2013, 3:55:42 AM8/7/13
to julia...@googlegroups.com
What is the problem with having minus or space in a symbol? You'd have to generate the code using Expr() and symbol("var-with minus&space"), as the parser uses those characters as delimiters when it parses code. Errors should be given when you use a dictionary with keys that does not match the keyword arguments of the function, so there is no reason to give a different error if some of the characters should be invalid.

Ivar

Westley Hennigh

unread,
Aug 7, 2013, 3:04:41 PM8/7/13
to julia...@googlegroups.com
I think the issue is transparency for the user. If they have a dictionary and get an error when they call (I kind of like Elliot's name) `dictsplat()`, it's pretty obvious what the problem is. If they just use the dictionary of strings and we do the conversion for them it might not be as clear.

Jeff Bezanson

unread,
Oct 15, 2013, 9:46:17 PM10/15/13
to julia...@googlegroups.com

Although converting a string to a symbol is reasonably efficient, it's too much extra work to do for something like a function call where every cycle counts. You could easily end up doing this work repeatedly, for each one of many calls. It also might lead somebody to think strings are equally valid in this context, and use them exclusively, which I would consider a performance trap.

Edward Chen

unread,
May 14, 2015, 3:25:58 PM5/14/15
to julia...@googlegroups.com
To whom it may concern:

I am having trouble adding elements to my dictionary and then passing it into a function:

function f(;kw...)
    for (k,v) in kw
        println(k,",",v)
    end
end 
d = {:a => 1, :b => "c"} # this works
# d = Dict() # but this line
# d["d"] = 2 # with this fails
f(;d...)

Thanks,
Ed

Peter Brady

unread,
May 14, 2015, 4:08:45 PM5/14/15
to julia...@googlegroups.com
I believe you want

d[:d] = 2

Peter.

Edward Chen

unread,
May 14, 2015, 4:11:31 PM5/14/15
to julia...@googlegroups.com
Ah yeah that works!

The wikibooks documentation is wrong then, maybe i'll correct it:

Reply all
Reply to author
Forward
0 new messages