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

feature request: kwargs for getindex #25631

Closed
vtjnash opened this issue Jan 18, 2018 · 10 comments
Closed

feature request: kwargs for getindex #25631

vtjnash opened this issue Jan 18, 2018 · 10 comments
Labels
parser Language parsing and surface syntax

Comments

@vtjnash
Copy link
Sponsor Member

vtjnash commented Jan 18, 2018

Current behavior is to treat = as assignment inside []

julia> Meta.@lower x[a=1] = 3
:(begin
      a = 1
      setindex!(x, 3, 1)
      return 3
  end)

But for consistency with (), I would like to have it parse as a kwcall

setindex!(x, 3; a=1)

Currently, that has to be written:

x[(a = 1,)] = 3

which is much less nice.

@stevengj stevengj added the breaking This change will break code label Jan 19, 2018
@stevengj
Copy link
Member

I've used things like a[i += 1] to simultaneously increment a counter and do getindex or setindex, but presumably this would continue to work, since keywords happen in parsing and += is dealt with in lowering.

@StefanKarpinski
Copy link
Sponsor Member

Agree, this seems like a really bizarre misfeature request to me. The fact that indexing is lowered to a function call is incidental, indexing syntax is not function call syntax.

@roshanshariff
Copy link

I wanted to use this syntax for multidimensional arrays with "named" dimensions (as in the Python library xarray). So if M is a two-dimensional array with dimensions named x and y, I'd like to be able to write M[x=3, y=2] to index the (3, 2) element. Currently there's no way to use the subscript notation for getindex(A; dims...).

@JeffBezanson JeffBezanson added the parser Language parsing and surface syntax label May 30, 2018
@JeffBezanson
Copy link
Sponsor Member

That does seem more useful to me than doing an assignment inside an indexing expression (a[b+=1] is indeed a separate case and would continue to work).

@JeffBezanson JeffBezanson added the triage This should be discussed on a triage call label May 30, 2018
@Keno
Copy link
Member

Keno commented May 31, 2018

I find that argument convincing.

@martinholters
Copy link
Member

In function calls, we also allow e.g. x+=1 but---obviously---treat x=1 as a keyword argument, so doing the same for indexing seems fine.

@JeffBezanson JeffBezanson added this to the 1.0 milestone Jun 7, 2018
@JeffBezanson JeffBezanson removed the triage This should be discussed on a triage call label Jun 7, 2018
@StefanKarpinski
Copy link
Sponsor Member

Is this on the wrong milestone?

@JeffBezanson
Copy link
Sponsor Member

No; for 0.7 we added a deprecation warning, and for 1.0 the new desired behavior (lowering to kwargs) needs to be added to replace it.

@mbauman
Copy link
Sponsor Member

mbauman commented Jan 28, 2019

Re-opening to track the possibility of adding this feature in 1.x now that the old behavior has been deprecated.

@mbauman mbauman reopened this Jan 28, 2019
@c42f
Copy link
Member

c42f commented Oct 1, 2019

It appears this feature was implemented in 1.0 as part of #28366:

julia> dump(:(x[a=1]))
Expr
  head: Symbol ref
  args: Array{Any}((2,))
    1: Symbol x
    2: Expr
      head: Symbol kw
      args: Array{Any}((2,))
        1: Symbol a
        2: Int64 1

@c42f c42f closed this as completed Oct 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser Language parsing and surface syntax
Projects
None yet
Development

No branches or pull requests

9 participants