Skip to content

const constructors can't have asserts #24841

Closed
@Hixie

Description

@Hixie

In Flutter we are trying very hard to make our Widget subclasses all have const constructors, because we want to make that entire class hierarchy be immutable by convention, and the rule "you have to have a const constructor" makes it easy to verify that you didn't screw that up. (It also means that widget subtree descriptions, even relatively elaborate ones, can end up being all evaluated at compile time rather than runtime.)

We are also trying very hard to catch errors in arguments passed to Widget classes as close to the relevant source line as possible. The best way we've found to do this is to have asserts in the constructors. We're checking all kinds of things -- that your arguments aren't null, that they're in the right numeric range, that you have provided either this or that argument but not both (or not neither), that the map you passed in has the important keys we need, etc.

Currently these two desires are in conflict. You can't have any asserts if your constructor is const, even if the assert condition itself is a compile-time-evaluatable const expression.

(Track implementation here: #27141)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).customer-fluttertype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions