Document strange downcast conventions used in the library, add a function for downcasts
- Dominant language
- Dart
- Stars
- 572
- Forks
- 196
- Avg merge
- 1h 59m
- Merged PRs (30d)
- 2
Description
In protobuf library, for downcasting we use implicit cast syntax:
```
=
```
instead of explicit cast syntax:
```
[var|final] = as
```
The reason is because dart2js compiles these two differently, and the implicit cast syntax is [much more efficient](https://github.com/google/protobuf.dart/pull/579#issuecomment-1152130791).
(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
```dart
/// 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(dynamic value) => value;
```
Contributor guide
Assessment
This issue has not been assessed yet.