dart-lang / dart-lang/language

Proposal to add `throwIf` and `throwIfNot` like assert

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

Description

While working on one of my packages I had a lot of the typical blocks:

```Dart
if (condition)
{
throw (error);
}
```

so I created two functions:

```Dart
void throwIf(bool condition, Object error) {
if (condition) throw (error);
}

void throwIfNot(bool condition, Object error) {
if (!condition) throw (error);
}
```

And have to say it it's super expressive when used in code:

```Dart
throwIf(
instanceName == null,
ArgumentError.value(
T, "Object of type ${T.toString()} is not registered inside GetIt"),
);
throwIfNot(
!(((const Object() is! T) && instanceName != null)),
ArgumentError(
'GetIt: You have to provide either a type OR a name not both.'),
);
```

So I thought wouldn't it be possible to add that to the language?

Cheers and thanks for this awesome SDK

Contributor guide

Open the contributing guide

Research direction

No source file or test is named in the issue. Start by reviewing Dart's existing assert behavior and the language-change process, then define the semantics and specification impact of the proposed throwIf and throwIfNot functions. Done would require an accepted language design with corresponding implementation and validation work identified.

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
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.