google / google/protobuf.dart

Document strange downcast conventions used in the library, add a function for downcasts

Open
#712 3 comments 0 reactions 0 assignees View on GitHub
refactoring
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.