syntax for computed keyword argument names #7704

Closed
WestleyArgentum opened this Issue Jul 23, 2014 · 5 comments

Projects

None yet

3 participants

@WestleyArgentum
Member

Bit of a confusing title, sorry. Basically I've found myself in positions where I need to invoke a function with named args that I've been passed dynamically:

# like so...
fn_to_call(; some_arg = 123) = println("woooo: $some_arg")
arg_name = :some_arg
arg_val = 678

# I want to invoke my function! The syntax currently supported goes like so:
fn_to_call(; [(arg_name, arg_val)]...)

# Don't get me wrong, that's pretty sweet, but personally I think it could be even more straightforward... like:
fn_to_call(; $arg_name = arg_val)

As I'm writing this I'm starting to think this could cause horrible problems when defining functions inside of macros... but maybe I'm wrong. Either way I'd be interested to hear what people think!

@JeffBezanson
Member

It's a good idea to provide some syntax for this, as it would be pretty easy to support. However I agree that using $ could get confusing.

How about this: any expression after ; that is not of the form x = y gets evaluated, and is treated as a pair whose first element is the keyword and second element is the value. This fits very well with the existing keyword splicing syntax f(x; kw...). For example

fn(; (arg_name, arg_val))

or

pair = (arg_name, arg_val)
fn(; pair)
@JeffBezanson JeffBezanson changed the title from allow interpolation syntax for names of keyword args when invoking function? to syntax for computed keyword argument names Jul 23, 2014
@WestleyArgentum
Member

Cool! That would be pretty sweet

@JeffBezanson JeffBezanson added this to the 0.4 milestone Jul 24, 2014
@JeffBezanson JeffBezanson added a commit that closed this issue Sep 29, 2014
@JeffBezanson JeffBezanson allow fully-evaluated keyword argument pairs. closes #7704
anything in an argument list after a ; that's not of the form `a=b` will
be evaluated and expected to be some kind of pair of a symbol and value.
a0720ce
@stevengj
Member

Wouldn't it be better to use the new Pair syntax for this? i.e. you could call f(...; foo => bar, ...) instead of using (foo,bar).

Nevermind, this is automatically allowed because Pair is iterable... seems like it would be cleaner to use the Pair syntax in the documentation, though.

@JeffBezanson
Member

Yes, it's automatically allowed.

@WestleyArgentum
Member

I can't believe missed this getting implemented! Thanks so much @JeffBezanson!

@stevengj stevengj added a commit that referenced this issue Oct 29, 2014
@stevengj stevengj documentation for #7704 74a26da
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment