-
Notifications
You must be signed in to change notification settings - Fork 209
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
Should dynamic
type checks give rise to promotion when performed during declaration pattern matching?
#2846
Comments
dynamic
type checks give rise to promotion when performed during declaration pattern matching?
I'm always in favor of these changes. |
EDIT: I take this back. Implicit downcasts and explicit casts should not work the same way. I don't want an implicit downcast to introduce a type of interest. And I want to unify coercions more. I think we should fix the non-record behavior to match. We generally promote on a successful void main() {
dynamic pi = 3.14;
final double v1 = pi as double;
int v2 = pi;
} This works and promotes It is a potentially breaking change, because we downcast away from void main() {
dynamic pi = 3.14;
final num v1 = pi;
print(pi.toStringAsFixed(5)); // only available on double, works today.
} |
Note that #1362 contains a discussion about the same topic in pre-pattern Dart. The support for not promoting was considerably stronger than the support for promoting, but the discussion wasn't closed entirely. Nevertheless, it seems quite inconsistent that we do get a promotion when the declaration is I think we should ask for an implementation of the non-promoting semantics. @dart-lang/language-team? |
I feel fairly strongly that the promotion behavior should be the same between non-pattern and pattern declarations. And I find promoting based on implicit downcasts from |
As of dart-lang/sdk@adbf363, |
Consider the following program:
This program is rejected by the analyzer and the CFE (commit bedcd576560c386e6930573e97ee82596b69fdfe) with the following compile-time errors:
This seems to imply that the type of
pi
has been promoted by being in the continuation of the pattern declaration wherev1
is declared. This may seem normal, but it would be a somewhat inconsistent approach to promotion becausepi
isn't promoted in this very similar program with no patterns:In this case there is no compile-time error (but, of course, the initialization of
v2
throws at run time), which is a consequence of the fact thatpi
still has typedynamic
when it is used to initializev2
.So do we treat the dynamic check associated with a cast from
dynamic
such that it promotes when it occurs as part of a pattern variable initialization, but not when it is part of a regular local variable initialization? We can certainly do that, but I think it's sufficiently surprising to require a clarification and a decision.Was it just an oversight, the fact that we don't promote
pi
when it is subject to a dynamic type check because it has typedynamic
? Maybe we should just do it in all cases where such type checks are performed? It would be a breaking change (e.g. dynamic invocations onid
that just happened to succeed would now be compile-time errors), but it is hardly very breaking in practice, and it probably "good breakage" because it turns dynamic invocations into statically checked ones.@dart-lang/language-team, WDYT?
The text was updated successfully, but these errors were encountered: