Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add a truthy operator #20834

Closed
Zectbumo opened this issue Sep 6, 2014 · 3 comments
Closed

Add a truthy operator #20834

Zectbumo opened this issue Sep 6, 2014 · 3 comments
Assignees
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

Comments

@Zectbumo
Copy link

Zectbumo commented Sep 6, 2014

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?

@DartBot
Copy link

DartBot commented Sep 6, 2014

This comment was originally written by @zoechi


Your examples can be simplyfied a bit (not shorter but IMHO easier to interpret)
 if (myStr != null && myStr.isNotEmpty);
 if ((myStr ?? '').isNotEmpty);
 if (myStr?.isNotEmpty ?? false));
 if ((myStr ?? '').isNotEmpty);

I think http://dartbug.com/22 is related too.
Having non-nullable types would take some burden off checking for truthiness.
I didn't like these truthy checks in JavaScript much. I prefer how in Dart one has to be more explicit. Being explicit could be simplified though.

  @­nonnull String myStr; // or whatever the syntax could be
  // all your examples would reduce to
  if (myStr.isNotEmpty)

@lrhn
Copy link
Member

lrhn commented Sep 6, 2014

Added Area-Language, Triaged labels.

@gbracha
Copy link
Contributor

gbracha commented Dec 23, 2014

Set owner to @gbracha.
Added WontFix label.

@Zectbumo Zectbumo added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Dec 23, 2014
@kevmoo kevmoo added 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 and removed priority-unassigned labels Mar 1, 2016
This issue was closed.
# for free to join this conversation on GitHub. Already have an account? # to comment
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
Projects
None yet
Development

No branches or pull requests

5 participants