Skip to content

Allow compile-time constant index operations on constant string, list and maps. #3059

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

Open
lrhn opened this issue May 15, 2012 · 10 comments
Open
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). core-n type-enhancement A request for a change that isn't a bug

Comments

@lrhn
Copy link
Member

lrhn commented May 15, 2012

With constant maps, and to a lesser degree lists, it's sometimes relevant to do refer to a constant entry of such a map.

I suggest allowing expressions on the form a[b] as compile-time constants when either a is a List or String and b is an integer, or a is a Map and b is a String.

@gbracha
Copy link
Contributor

gbracha commented May 16, 2012

Makes sense to me (assuming a and b are constants).


Set owner to @gbracha.
Added Accepted label.

@gbracha
Copy link
Contributor

gbracha commented May 24, 2012

Added this to the Later milestone.

@kasperl
Copy link

kasperl commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@kasperl
Copy link

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@gbracha
Copy link
Contributor

gbracha commented Jan 3, 2015

Issue #8885 has been merged into this issue.

@lrhn lrhn added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Jan 3, 2015
@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
@lucaslcode
Copy link

+1

@alan-knight
Copy link
Contributor

Here's a concrete use case where this would be quite nice. Not entirely fleshed out, but it seems like it would be workable.

With Intl, you write the message text directly in the code. e.g.

  get hello => Intl.message('hello world', desc: 'A greeting to the world from a very simple program');

The desc parameter is only used at message extraction time, and the library is written so that dart2js knows it can completely remove it during compilation. But the actual text is used as a lookup key for the translation, so we can't remove it. And that wastes a bunch of space, because we have the English text included in every version, even if it's never going to be displayed.

We could avoid this by instead having you write the English text in a separate resource file, create some ID for it, and use that in the code. But that's a lot worse experience.

However, I can compute some known value based on the text, and probably do for translation purposes. And if I was able to de-reference a const, then it seems like I could do something like

    get hello => Intl.message('hello world', desc: 'A greeting to the world from a very simple program').toString();
...
   class Intl {
      const Intl.message(String text, String desc): _id = messageHashes[text];

and have generated code

    const messageHashes = {
       'hello world' : 0xabcdefg,
    }

And if the system allowed me to do that dereference in an initializer, and was smart enough to know that the messageHashes map was never referenced from anywhere except a const constructor and could be discarded, then we could optimize away all that English text and just store ids.

@lrhn lrhn added core-n and removed P2 A bug or feature request we're likely to work on labels Dec 6, 2018
@Levi-Lesches
Copy link

Hi! Any progress on this? I think it's kinda reasonable to expect a const string on a const Map to be const.

@yudi-tan
Copy link

It's been awhile since this feature has been talked about -- any updates on the progress?

@lrhn
Copy link
Member Author

lrhn commented Apr 18, 2020

There are no current plans to extend the scope of compile-time constant expressions.

When we do something in this context, it's driven by necessity from other changes to the language, not a wish to actually make more expressions compile-time constant.

Incrementally adding small syntax extensions to the constant rules is not something that is giving the most bang for the buck. I think that, with the current priorities for Dart, a complete revamp of the constant sub-language and behavior is worth investigating, rather than incrementally growing the existing one on a case-by-case basis.

copybara-service bot pushed a commit that referenced this issue Jun 8, 2022
…3 revisions)

https://dart.googlesource.com/dartdoc/+log/f5bcc4bd46f4..b61647bc3833

2022-06-08 [email protected] Pin github actions to a hash (#3056)
2022-06-06 49699333+dependabot[bot]@users.noreply.github.com Bump ossf/scorecard-action from 1.1.0 to 1.1.1 (#3059)
2022-06-06 49699333+dependabot[bot]@users.noreply.github.com Bump github/codeql-action from 2.1.11 to 2.1.12 (#3057)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/dart-doc-dart-sdk
Please CC [email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Dart Documentation Generator: https://github.com/dart-lang/dartdoc/issues
To file a bug in Dart SDK: https://github.com/dart-lang/sdk/issues

To report a problem with the AutoRoller itself, please file a bug:
https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md

Tbr: [email protected]
Change-Id: Ia70e7e622ec520a5f8f19d3bfc51608c8d6f5c70
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/247469
Reviewed-by: Devon Carew <[email protected]>
Commit-Queue: Devon Carew <[email protected]>
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). core-n type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

8 participants