dart-lang / dart-lang/language

Const asserts

Open
#625 5 comments 11 reactions 0 assignees View on GitHub
enhanced-const
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Today I encountered an issue where I needed to ensure that some constants are edited correctly to prevent a runtime error. While it's possible to write a StaticAssert class like this:

```dart
class StaticAssert {
const StaticAssert(bool value, [String message = 'Assertion failed']) : assert(value, message);
}
```

Using it is somewhat difficult, since the analyzer complains about unused variables:

```dart
const _asserts = [
_StaticAssert(a <= b),
_StaticAssert(c <= b),
];
```

It would be a bit better if you could write a top level assert:

```dart
const assert(a <= b);
const assert(c <= b);
```

Which is sorta like `static_assert` in C++11.

Contributor guide

Open the contributing guide

Research direction

Start with the StaticAssert example and the proposed top-level const assert syntax in the issue. Determine the language-design rules and specification changes needed for compile-time assertions, including how constant expressions and messages should behave; done means the proposal has a resolved design.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.