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

Indexing into a constant list should be a constant expression. #8885

Closed
munificent opened this issue Mar 2, 2013 · 2 comments
Closed

Indexing into a constant list should be a constant expression. #8885

munificent opened this issue Mar 2, 2013 · 2 comments
Assignees
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

@munificent
Copy link
Member

munificent commented Mar 2, 2013

The minimal repro for the issue is:

const LIST = const ['a', 'b', 'c'];
const B = LIST[1]; // ERROR

Given that the list and the index are both constants, it seems reasonable that it should be a valid constant expression. But currently this gives an error.

I know I can (and will for now) rely on canonicalization to get around this, but it's particularly egregious in my real-world example:

class CompressionLevel {
  static const NONE     = CompressionLevel.LEVELS[0];
  static const FASTEST  = CompressionLevel.LEVELS[1];
  static const SMALLEST = CompressionLevel.LEVELS[9];
  static const DEFAULT  = CompressionLevel.LEVELS[6];

  static const LEVELS = const [
    //                       id  good lazy nice chain
    const CompressionLevel._( 0,  0,     0,   0,    0, _STORED),
    const CompressionLevel._( 1,  4,     4,   8,    4, _FAST),
    const CompressionLevel._( 2,  4,     5,  16,    8, _FAST),
    const CompressionLevel._( 3,  4,     6,  32,   32, _FAST),
    const CompressionLevel._( 5,  4,     4,  16,   16, _SLOW),
    const CompressionLevel._( 6,  8,    16,  32,   32, _SLOW),
    const CompressionLevel._( 7,  8,    16, 128,  128, _SLOW),
    const CompressionLevel._( 8,  8,    32, 128,  256, _SLOW),
    const CompressionLevel._( 9, 32,   128, 258, 1024, _SLOW),
    const CompressionLevel._(10, 32,   258, 258, 4096, _SLOW)
  ];

  final int _index;
  final int _goodLength;
  final int _maxLazy;
  final int _niceLength;
  final int _maxChain;
  final int _func;

  const CompressionLevel._(this._index, this._goodLength, this._maxLazy,
                           this._niceLength, this._maxChain, this._func);
}

Here, it would be tedious and error-prone to repeat all of the fields for NONE, FASTEST, SMALLEST, and DEFAULT.

@gbracha
Copy link
Contributor

gbracha commented Mar 2, 2013

I have no problem with this. I do question whether we want to keep adding minor features at this time; thi seems like 2.0 thing to me.


Added Accepted label.

@gbracha
Copy link
Contributor

gbracha commented Jan 3, 2015

Added Duplicate label.
Marked as being merged into #3059.

@munificent munificent added Type-Enhancement 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 labels Jan 3, 2015
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
@munificent munificent reopened this Dec 10, 2016
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

3 participants