Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax for computed keyword argument names #7704

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

syntax for computed keyword argument names #7704

WestleyArgentum opened this issue Jul 23, 2014 · 5 comments
Milestone

Comments

@WestleyArgentum
Copy link
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
Copy link
Sponsor 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 allow interpolation syntax for names of keyword args when invoking function? syntax for computed keyword argument names Jul 23, 2014
@WestleyArgentum
Copy link
Member Author

Cool! That would be pretty sweet

@stevengj
Copy link
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
Copy link
Sponsor Member

Yes, it's automatically allowed.

@WestleyArgentum
Copy link
Member Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants