dart-lang / dart-lang/language

Consider changing context type for index expressions

Open
#2,445 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

type-inference
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 for index is the type of the index parameter to the target's operator [].
  • In a simple indexed write (target[index] = value), the context type for index is the type of the index to the target's operator []=.
  • In a compound read/write (e.g. target[index] += value, target[index] ??= value, ++target[index], target[index]++, the context type for index is the type of the index parameter to the target's operator [].

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.