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 const instance fields as const expressions on const objects. #16547

Open
lrhn opened this issue Feb 5, 2014 · 3 comments
Open

Allow const instance fields as const expressions on const objects. #16547

lrhn opened this issue Feb 5, 2014 · 3 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug

Comments

@lrhn
Copy link
Member

lrhn commented Feb 5, 2014

We currently disallow const on instance fields because we don't have a good semantics for them.

Consider allowing

class C {
  const int foo;
  const C(this.foo);
}

The instance const field will act just as a final field, except when initialized by a const invocation of a const constructor (i.e., except when on an actual compile-time constant).

Then we can allow expressions like x.foo to be compile time constant expressions if x is a compile time constant expression with a value that has a const instance field named foo.

This will allow changing a static-only class to an object, and still expose the same compile time constants.

Other uses could be an enum implementation that assigns a unique id to each enum object, and then allowing switching on the id instead of on the object by writing case FOO.id:.

This breaks the field/getter symmetry, so perhaps we can also allow:

const get x => <potential instance compile time constant expression>

where the expression must be a compile time constant expression potentially containing this.x references to const fields (just as potential const expressions in constructors can refer to const constructor parameters).

@gbracha
Copy link
Contributor

gbracha commented Aug 26, 2014

It looks like it would all work, though I am loath to complicate const and/or getters any further.


Set owner to @gbracha.
Added Accepted label.

@DartBot
Copy link

DartBot commented Aug 26, 2014

This comment was originally written by @seaneagan


Yes, const is currently pretty complicated. But the thing that's complicated about it is all the restrictions and special cases about what can and cannot be const. The goal should be to remove the restrictions and special cases from the language by allowing constants to be defined as much as possible at the library level. See issue #18241 for a more comprehensive approach.

@DartBot
Copy link

DartBot commented Feb 6, 2015

This comment was originally written by @zoechi


It looks like this is the error I run into with this code

class SomeEnum {
  static const SomeEnum FIRST = const SomeEnum._(1, 'FIRST');

  static const List<SomeEnum> values = const <SomeEnum> [FIRST,];

  final value;
  final name;
  const SomeEnum._(this.value, this.name);
}

void main() {
  const x = SomeEnum.FIRST.name; // <= 'const' variables must be a const value
}

@lrhn lrhn added Type-Enhancement P3 A lower priority bug or feature request area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Feb 6, 2015
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed accepted labels Feb 29, 2016
@lrhn lrhn removed the P3 A lower priority bug or feature request label Aug 31, 2020
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). type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants