dart-lang / dart-lang/language
[Feature Request] - Contracts
- 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
Assessment
This issue has not been assessed yet.