Skip to content

member of const object should be member as well #23361

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

Closed
DartBot opened this issue May 3, 2015 · 5 comments
Closed

member of const object should be member as well #23361

DartBot opened this issue May 3, 2015 · 5 comments
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

@DartBot
Copy link

DartBot commented May 3, 2015

This issue was originally filed by halfjui...@gmail.com


For example, the following example should work. But it's not right now in the IDE.

class Config {
  final String name;
  const Config(this.name);
}

class App {
  static const config = const Config('hello');
}

@RandomAnnotation(fieldNeedsToBeConst = App.config.name)
class Foo {
}

@DartBot
Copy link
Author

DartBot commented May 3, 2015

This comment was originally written by @zoechi


What Dart version do you use?

@lrhn
Copy link
Member

lrhn commented May 3, 2015

While I agree that it (or something similar) should work, it's not currently valid Dart code.

Reading a property of a const object is not itself a compile-time constant expression. That means that "App.config.name" is not a compile-time constant expression due to the ".name", and it can't be used as parameter to the const expression @­RandomAnnotation(...).

So, working as intended. I'll mark this as an enhancement request for the language.


Removed Type-Defect label.
Added Type-Enhancement, Area-Language, Triaged labels.

@DartBot DartBot added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels May 3, 2015
@rkj
Copy link

rkj commented Nov 25, 2015

Why reading a property of const object is not itself a const expression? Any similar syntax to achieve the same? This makes impossible to do a case switch on a ProtobufEnum (https://www.dartdocs.org/documentation/protobuf/0.5.0%2B1/protobuf/ProtobufEnum-class.html), and that makes a lot of sad if/else clauses...

@lrhn
Copy link
Member

lrhn commented Nov 25, 2015

I can't say why it isn't valid - the short answer is "because it's not in the spec".

On possible reason for rejecting it is that it will break the getter/field equivalence.
Currently fields are specified as being accessed through implicit getters, and all o.foo accesses are getter calls. That means that you can replace a field with a getter and vice versa without changing the way a class can be used.

If you can read final fields of a const object as a const expression, and you can't read a getter as a const expression (which you definitely can't, const expressions never execute code), then the field/getter equivalence is broken, and you can't change a field to a getter any more.

That's at odds with the design of the language, so we don't want to add that, even if it can be convenient occasionally.

A solution that has been suggested is to allow const instance fields. They are like final fields except that they can be read as compile time constants, and this way the class author has to explicitly opt in to the field being const. After that, it might even be possible to add const getters - getters with bodies that are potentially constant expressions, but with access to const fields of the same object - but that requires a way to avoid recursion.

@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed triaged labels Mar 1, 2016
@munificent
Copy link
Member

Closing as a duplicate of #16547. This issue describes the problem while that one describes a potential solution.

@munificent munificent added the closed-duplicate Closed in favor of an existing report label Dec 15, 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

5 participants