Skip to content

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

@osa1

Description

@osa1

In protobuf library, for downcasting we use implicit cast syntax:

<type> <var> = <expr>

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactoringNon-user-facing changes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions