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

Const error when chaning method invocations #40644

Closed
dnfield opened this issue Feb 14, 2020 · 3 comments
Closed

Const error when chaning method invocations #40644

dnfield opened this issue Feb 14, 2020 · 3 comments

Comments

@dnfield
Copy link
Contributor

dnfield commented Feb 14, 2020

I'm not sure if this is a bug or not, but it seems strange to me. The following program will fail to compile:

class Foo {
  const Foo();
  
  void resolve() {
    print('resolve');
  }
}

void main() {
  const Foo foo = Foo()..resolve();
}

But final Foo foo = const Foo()..resolve(); is ok, and const Foo foo = const Foo(); foo.resolve(); are both ok. I would expect the compiler to desugar the failing version into the last version there.

@dnfield
Copy link
Contributor Author

dnfield commented Feb 14, 2020

Duplicate of dart-lang/language#542 (comment)

@dnfield dnfield closed this as completed Feb 14, 2020
@lrhn
Copy link
Member

lrhn commented Feb 17, 2020

I don't think this is a duplicate of #542, but it is similar.

The reason this program does not compile is that Foo()..resolve() is not a compile-time constant expression. It cannot be because calling a method never is.

The problem is not (as in #542) that the initializer refers to the new variable foo before it's initialized. There is also no risk that the const Foo() object is seen in an uninitialized state because it is immutable when created.

Rather, the problem is that const declarations really do not occur as part of the normal evaluation flow.

For a local variable, we could perhaps find a way to split Foo()..resolve() into a constant part and a run-time part which is evaluated ... every time the declaration is evaluated? Even if we find a way to specify it meaningfully, it's a very specialized feature which is unlikely to pay for its own complexity cost.
It would not work for a top-level const variable. If you did const foo = Foo()..resolve(); at top-level (or as a static), it's very, very unclear when and how often the resolve would get called.

@dnfield
Copy link
Contributor Author

dnfield commented Feb 17, 2020

I assumed because of how constant evaluation is described in the linked issue, this would require the other

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants