dart-lang / dart-lang/language
Consider changing context type for index expressions
Nobody has claimed this yet.
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
The current front end rule for the context of index expressions (that is, the argument to []) is currently:
- In a simple indexed read (
target[index]), the context type forindexis the type of the index parameter to the target'soperator []. - In a simple indexed write (
target[index] = value), the context type forindexis the type of the index to the target'soperator []=. - In a compound read/write (e.g.
target[index] += value,target[index] ??= value,++target[index],target[index]++, the context type forindexis the type of the index parameter to the target'soperator [].
The first two bullets make a lot of sense. The third bullet, not so much. There's been a lot of discussion about this at https://github.com/dart-lang/sdk/issues/49106, but since that issue is primarily about a problem with the migration tool, I'm going to try to capture the rough consensus (as I understand it).
There seemed to be general support for the idea of using the GLB of the index parameter types from both operator [] and operator []=. This would work well for maps, where the index type of operator []= is a subtype of the index type of operator []. Probably a lot of classes that implement these two operators are providing map-like functionality, so this choice will probably work well for them too.
There was also some discussion about whether we should emit a diagnostic if the user doesn't give the two operators compatible index types. It's not immediately clear to me what "compatible index types" means, though. Do we require the same relationship that Map has (index type of operator []= is a subtype of the index type of operator [])? Or do we just require that either of them be a subtype of the other? Also, should the diagnostic be an error or a lint?
Similarly, do we want a diagnostic relating the return type of operator [] to the type of the "value" parameter of operator []=? This would be similar to the diagnostic we already have for how getter and setter types must relate. And if we do want to do this, should it be an error or a lint?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the three current index-expression context rules in this issue, then review the discussion in dart-lang/sdk#49106. Resolve whether compound expressions should use the GLB of both index parameter types and whether incompatible index or getter/setter value types need diagnostics; done means the language change and diagnostic policy are agreed and specified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100