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

Allow multiple parameters for the [] and []= operators. #8980

Closed
lrhn opened this issue Mar 7, 2013 · 4 comments
Closed

Allow multiple parameters for the [] and []= operators. #8980

lrhn opened this issue Mar 7, 2013 · 4 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug

Comments

@lrhn
Copy link
Member

lrhn commented Mar 7, 2013

We could allow multiple parameters on the [] and []= operators, e.g.,

class Matrix2D<V> {
  V operator[](int x, int y);
  void operator[]=(V value, int x, int y);
}

That would allow multi-dimensional lookups and assignments with a good-looking syntax. Usage would be the expected:

matrix[2,4] = matrix[1,3] + 1;

Using the wrong number of parameters for an object would be a noSuchMethod call, just as for a similar methods.

The alternative is to write "get" and "set" methods, which isn't as readable.

The necessary grammar changes is to allow the same arguments between [] as between () in a call, and relax the requirements on arguments for operator[] and operator[]=.
I put the value of operator[] first, so that it's possible to use named parameters, e.g. foo[x:42, y:12] = 10.

We can also disallow optional parameters to begin with.

(This was not my idea, but I liked it when I heard it!)

@munificent
Copy link
Member

I remember discussing this with Gilad a long time ago, but I guess I never filed a bug. Given that the "() operator" (i.e. "call") allows arbitrary method signatures, it seems inconsistent that [] does not. It's certainly a painful limitation when you want to define any kind of matrix or multi-dimensional array type (see: every game ever written).

I would really really like this feature.

@gbracha
Copy link
Contributor

gbracha commented Aug 25, 2014

FTR: There is no () operator, and therefore no inconsistency. You want to introduce an inconsistency by making certain operators special as a syntactic sugar. Maybe someday, but not very soon.


Set owner to @gbracha.
Added Accepted label.

@lrhn
Copy link
Member Author

lrhn commented Dec 4, 2014

The current request doesn't require varargs - it just treats everything between '[' and ']' as arguments to operator[], just as if you had written '(' and ')' to specify arguments to call.

It will work fine for two-dimensional matrices (operator[](x,y)) but not for arbitrary-dimension matrices implemented in one class.

@lrhn lrhn added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Dec 4, 2014
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed accepted labels Feb 29, 2016
@lrhn lrhn removed the P2 A bug or feature request we're likely to work on label Aug 31, 2020
@munificent munificent added the closed-duplicate Closed in favor of an existing report label Sep 30, 2022
@munificent
Copy link
Member

Closing as duplicate of dart-lang/language#2534.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants