In protobuf library, for downcasting we use implicit cast syntax:
instead of explicit cast syntax:
[var|final] <var> = <expr> as <type>
The reason is because dart2js compiles these two differently, and the implicit cast syntax is much more efficient.
(AOT and JIT treat both the same way)
For a reader it won't be obvious why we do this. We should create a function for downcasting that uses implicit cast syntax, and document why we use it. @mkustermann suggests
/// Cast value down using implicit `as` check.
///
/// This will impact dart2js which does not perform implicit `as` in -O4 (but does perform explicit `as` checks)
@pragma('vm:prefer-inline')
@pragma('dart2js:Inline')
T downcastAs<T>(dynamic value) => value;