Add a truthy operator #20834
Labels
area-language
Dart language related items (some items might be better tracked at github.com/dart-lang/language).
closed-not-planned
Closed as we don't intend to take action on the reported issue
type-enhancement
A request for a change that isn't a bug
Javascript and other languages have truthy tests that results in clean code:
if (myStr);
Is it possible to have something reads better in Dart than any of the following:
if (myStr != null && !myStr.isEmpty);
Even if we employed issue #41 and issue #1236 it gets worse:
if (!(myStr?.isEmpty ?? true));
if (!(myStr ?? '').isEmpty);
I wouldn't want to use any of those patterns. I would rather implement my own method:
http://pastebin.com/0bzDixFc
if (isTruthy(myStr));
So, how about a truthy operator instead:
if (!!myStr);
and maybe an abstract class to go along with it:
abstract class Truthy {
bool operator !!();
}
Will Object implement Truthy?
The text was updated successfully, but these errors were encountered: