This repository was archived by the owner on Feb 22, 2018. It is now read-only.
This repository was archived by the owner on Feb 22, 2018. It is now read-only.
improve type promotion #274
Closed
Description
There are some cases where type promotion won't work. Found while investigating the state of #31.
NOTE: My current thought here is that we should file a DEP for pattern matching, rather than try and hack type promotion into something sensible. The feature as is seems to cause a lot of user confusion.
It roughly falls into a few buckets:
- if the original type of
T
ofv
wasdynamic
, the testv is SomeType
won't promote. This leads to some suboptimal codegen in SDK (see strong mode should allow type promotion from dynamic dart-lang/sdk#25486) - if the tested type
S
is not a subtype ofT
, the test won't promote. You'd have to track a union type. - the type promotion is only valid in the body/else clause of that if statement. There's no analysis of the rest of the method, meaning patterns like
if (v is! S) return;
don't affect the type ofv
in the rest of the method body. - from Sky, we've heard they would like to do
assert(v is S);
and havev
be treated as S for the rest of the method. Sort of like an unchecked (in production mode) cast, similar toS v2 = v
but not requiring a second variable name to be introduced. - from package:yaml, assignment to the variable in a closure will disable all promotion, this is true even if that closure hasn't been defined yet (and therefore, cannot possibly mutate the variable).
- Constrained generic parameter like
T extends Event
, not promoting to a subtype of Event likeInputEvent
, see 'is' check in a trinary operator should do type promotion #327