Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add Existential operator support #5630

Closed
DartBot opened this issue Oct 3, 2012 · 3 comments
Closed

Add Existential operator support #5630

DartBot opened this issue Oct 3, 2012 · 3 comments
Labels
closed-duplicate Closed in favor of an existing report

Comments

@DartBot
Copy link

DartBot commented Oct 3, 2012

This issue was originally filed by demis.bell...@gmail.com


It would be nice if we could prevent null ref errors by adding Existential operator support.

e.g. Instead of
   if(foo != null && foo.bar != null && foo.bar.baz != null) {
      print (foo.bar.baz.monkey);
   }

you have

   print foo?.bar?.baz?.monkey

@DartBot
Copy link
Author

DartBot commented Oct 3, 2012

This comment was originally written by dha...@google.com


Are you suggesting that the ?. operator skip the entirety of the current statement if it attempts to dereference null? Otherwise those two snippets have different semantics.

@munificent
Copy link
Member

Added Duplicate label.
Marked as being merged into #41.

@DartBot
Copy link
Author

DartBot commented Oct 3, 2012

This comment was originally written by demis.bel...@gmail.com


Apologies, I copied the example from this previous thread:
https://groups.google.com/a/dartlang.org/forum/?fromgroups=#!topic/misc/eqfR585eDVY

Here's a clearer example in CoffeeScript:

doSomethingWithPostcode = (x) => alert(x)
 
doSomethingWithPostcode(customer?.address?.postcode) #this will still call with undefind
 
doSomethingWithPostcode(customer.address.postcode) if customer?.address?.postcode #this wont call the method at all

The generated JavaScript when pasted in http://coffeescript.org/ becomes:

var doSomethingWithPostcode, _ref, _ref1,
  _this = this;
 
doSomethingWithPostcode = function(x) {
  return alert(x);
};
 
doSomethingWithPostcode(typeof customer !== "undefined" && customer !== null ? (_ref = customer.address) != null ? _ref.postcode : void 0 : void 0);
 
if (typeof customer !== "undefined" && customer !== null ? (_ref1 = customer.address) != null ? _ref1.postcode : void 0 : void 0) {
  doSomethingWithPostcode(customer.address.postcode);
}

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-duplicate Closed in favor of an existing report
Projects
None yet
Development

No branches or pull requests

2 participants