-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Annotation that the argument for a parameter should be a constant #29381
Comments
If we don't want to add language support to specify that a parameter is required to be a constant value (such as by allowing the |
Is there anyway to repurpose It might be confusing to have multiple annotations that all mean "please use const". |
I wouldn't have a problem using |
Constant objects in Dart are not intrinsically special. They are objects just like any other instance of the same class. They may have been created at runtime and then canonicalized (that's what the dev-compiler does). It's easier to say that an expression must be a constant expression than it is to say that a value must be constant. It's technically possible to mark a parameter as Generality would also suggest that All in all, I don't see the benefit of such a feature as outweighing the costs in complexity. |
Have we decided that this isn't worth a change in the language? Should we move back to area-analyzer to implement a static check outside of the language? |
We have not decided either way, but it hasn't really been high enough on our list of priorities to be discussed at all. I can see the reasoning for wanting to give the user a hint that something should probably be constant, but it doesn't really scale to a language feature. If we mark a method or function parameter declaration as requiring a constant argument, then what happens if we want to treat that function/method-tear-off as a first class function value? Should it retain the property that you can only call it with a constant argument? If not, it doesn't feel like a proper language feature, more like a hint or suggestion. If so, then we need "constant only" parameters in function types, and some subtyping relation between constant-requiring functions and non-constant requiring functions. That means you can't call Would we then need to allow It would probably work, but the complication of the type system is more costly than I think the feature is actually worth, so I don't see this actually happening. This would likely be more intrusive than So, after this contemplation, I don't see this as a viable language feature for Dart. Adding it as an analyzer hint does seem more reasonable. It's not required to be safe or complete, just to heuristically help people do the right thing in the common case. |
Do we want to be able to apply the annotation to an operator ( Do we want to be able to apply the annotation to a setter ( |
The CL for this feature is here in case of any comments. |
I would apply the annotation to operator and setter parameters. Consider whether we'd want: @mustBeConst
String id = ""; to apply the |
@mosuem and I discussed whether we want propagate consts, and this can lead to a space explosion as detailed in (dart-lang/language#2776 (comment)). Also, when doing propagation, we quickly get into the question whether control flow should be supported and whether string concatination or any other const-able operation should be supported as discussed above. These can lead to more space explosion. For now, the use cases in dart-lang/native#153 don't seem to need it. |
constant. As const-only parameters are not planned, see dart-lang/language#1684, an annotation with analyzer support could help a bit at least, see #29381. The motivation is to enforce const arguments for methods annotated with `@ResourceIdentifier`, to be able to record the argument values at build time, see https://dart-review.googlesource.com/c/sdk/+/329961. Tested: pkg/analyzer/test/src/diagnostics/const_argument_test.dart Change-Id: I2b8d2dce0c899fc0caa4985d892a5d031c747521 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/357701 Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Lasse Nielsen <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Moritz Sümmermann <[email protected]> Reviewed-by: Marya Belanger <[email protected]>
This was landed in b321254. In the next Dart release (or sooner if you use an unstable release), the analyzer should trigger for |
Intl.message
has anexamples:
parameter. See https://www.dartdocs.org/documentation/intl/0.14.0/intl/Intl/message.htmlThe Map parameter is unused at run time.
The documentation does state: "The examples map must be a valid const literal map".
However, users don't always remember the
const
. It is very expensive to create a map for each call to message().What we need is a way to enforce that the map is a constant, perhaps an annotation on the parameter that the corresponding argument at the call site must be a constant.
The text was updated successfully, but these errors were encountered: