dart-lang / dart-lang/language

[Feature Request] - Contracts

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

Description

As it happens we write extension functions to check agains some validation, the compiler won't know that the value is validated inside a method as Not Null or something else and still will complain.

As example, on Kotlin there are Contracts.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.contracts/contract.html

On the code below, the compiler still will complain about Null check.

````dart
extension StringNullableExt on String? {
bool get isNullOrEmpty {
final instance = this;
return instance == null || instance.isEmpty;
}
}
````

````dart
String? somethingMayBeNull = arguments["something"];

if (somethingMayBeNull.isNullOrEmpty) return;

String notNull = somethingMayBeNull; // A value of type 'String?' can't be assigned to a variable of type 'String'.
````

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.