Skip to content

Constant Specification Update 2018 #61

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
mit-mit opened this issue Oct 25, 2018 · 3 comments
Closed

Constant Specification Update 2018 #61

mit-mit opened this issue Oct 25, 2018 · 3 comments
Assignees
Labels
enhanced-const Requests or proposals about enhanced constant expressions feature Proposed language feature that solves one or more problems
Milestone

Comments

@mit-mit
Copy link
Member

mit-mit commented Oct 25, 2018

This is the feature proposal for request #54. It improved the support for constant expressions, for example the ability to explicitly cast a value in a const expression:

class MyClass {
  final String value;
  const MyClass(Object o) : value = (o is! String) ? "$o" : (o as String);
}

The feature specification is:
https://github.com/dart-lang/language/blob/master/accepted/2.5/constant-update-2018/feature-specification.md

@mit-mit mit-mit added the feature Proposed language feature that solves one or more problems label Oct 25, 2018
@mit-mit mit-mit added this to the Dart 2.2 milestone Oct 25, 2018
@jodinathan
Copy link

jodinathan commented Jun 2, 2019

"https://dart-review.googlesource.com/c/sdk/+/36220 specified a number of changes to potentially constant and compile-time constant expressions;"

With this will it be possible to use functions in metadata?
eg:

class Foo {
  @Bar(() => 'foobar')
  String myProp;
}

today, the above is not possible, but this is

bar() => 'foobar';
class Foo {
  @Bar(bar)
  String myProp;
}

@lrhn
Copy link
Member

lrhn commented Jun 3, 2019

@jodinathan
No, there is no change to function literals in the listed changes, only small tweaks to include recently added operators and make the constant evaluation better match Dart 2 type semantics.

You have to do:

String kFooBar() => 'fooBar';
class Foo {
  @Bar(kFooBar)
  String myProp;
}

to tell the compiler that the function is a constant.

@mit-mit mit-mit modified the milestones: Dart 2.2, Dart 2.5 Sep 26, 2019
@mit-mit
Copy link
Member Author

mit-mit commented Sep 26, 2019

This shipped in Dart 2.5

@mit-mit mit-mit closed this as completed Sep 26, 2019
@eernstg eernstg added the enhanced-const Requests or proposals about enhanced constant expressions label Dec 1, 2021
@mit-mit mit-mit moved this to Done in Language funnel May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhanced-const Requests or proposals about enhanced constant expressions feature Proposed language feature that solves one or more problems
Projects
Status: Done
Development

No branches or pull requests

4 participants