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

Assignment gets read as a keyword #14

Open
mcabbott opened this issue Jul 10, 2020 · 3 comments
Open

Assignment gets read as a keyword #14

mcabbott opened this issue Jul 10, 2020 · 3 comments

Comments

@mcabbott
Copy link
Collaborator

Something which ought to work, I think:

mutable struct Bar x end; bars = [Bar(i) for i in 1:3];

@_ map(_.x, bars) # fine

@_ foreach(_.x = 2, bars) # ERROR: syntax: invalid keyword argument name "(_1,) -> begin

Originally from broadcasting setproperty!, here: JuliaLang/julia#36608

@c42f
Copy link
Owner

c42f commented Jul 13, 2020

Hm yes I think this should probably be made to work. I'm a little leery about possible visual ambiguities with keyword syntax — are there any super confusing combinations?

I'm thinking about things like f(_.x=1, y=_+1)... it's unambiguous but pretty confusing!

@mcabbott
Copy link
Collaborator Author

I hadn't realised that keywords worked at all, but they do, and may even be useful:

@_ sort(bars, by=-_.x)

Perhaps @_ f(_=_+1) should remain an error, but apart from that, testing the keyword's args[1] isa Symbol seems like it ought to be safe.

@c42f
Copy link
Owner

c42f commented Jul 14, 2020

Yes, keywords work intentionally

if ex isa Expr && (ex.head == :kw || ex.head == :parameters)

Perhaps @_ f(_=_+1) should remain an error, but apart from that, testing the keyword's args[1] isa Symbol seems like it ought to be safe.

I think the rule might be that if args[1] is a symbol which is not _, then it's a keyword. Otherwise it might be a function (or a syntax error) and we could try replace_(Expr(:(=), args...)). Then if that results in a closure use that instead of an Expr(:kw) ?

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

2 participants