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

make dealing with null less painful #21222

Closed
trinarytree opened this issue Oct 2, 2014 · 4 comments
Closed

make dealing with null less painful #21222

trinarytree opened this issue Oct 2, 2014 · 4 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug

Comments

@trinarytree
Copy link

problem 1: fallback values.
suppose you have a sequence of fallback values to use when you encounter null.
python example: v = x or y or z
js example: v = x || y || z;
dart example:
v = x != null ? x :
    y != null ? y :
    z;
yuk. notice how x and y are both repeated.
if x is a complex expression, perhaps an expression you aren't willing to evaluate twice, it gets even uglier:
v = complexExpressionX;
if (v == null) {
  v = complexExpressionY;
}
if (v == null) {
  v = complexExpressionZ;
}

and if v is a setter, perhaps something you don't want to call twice, it can get even uglier:
var tempV;
v = complexExpressionX;
if (v == null) {
  v = complexExpressionY;
}
if (v == null) {
  v = complexExpressionZ;
}
v = tempV;

and if this is inside an expression, where you can't have separate statements, e.g.
when trying to set a final value in a constructor, you need to get even uglier, e.g. by
creating a static method.

problem 2: navigating an object graph.
suppose you want to navigate down an object graph but just want null if any part is null.
groovy example: x?.y?.z
dart example:
x != null && x.y != null ? x.y.z : null;
and if the getters are expensive...
var tempZ;
var tempX = x;
if (tempX != null) {
  var tempY = tempX.y;
  if (tempY != null) {
    tempZ = tempY.z;
  }
}
and if this is inside an expression, where you can't have separate statements, it gets uglier.
e.g. you may need to extract a function. yuk.

groovy has null-coalescing and safe navigation operators: ?: and ?.
can dart get something like these to avoid the above pain?

@DartBot
Copy link

DartBot commented Oct 3, 2014

This comment was originally written by @zoechi


see also

@DartBot
Copy link

DartBot commented Oct 3, 2014

This comment was originally written by @seaneagan


for "problem 1: fallback values" see issue #1236

@kasperl
Copy link

kasperl commented Oct 8, 2014

Added Area-Language, Triaged labels.

@gbracha
Copy link
Contributor

gbracha commented Jan 2, 2015

This bug covers multiple requests, that are covered by several other bugs. I'm closing it asa duplicate. I have to pick one issue as a dup, I'll use 41.


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

@trinarytree trinarytree added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report labels Jan 2, 2015
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed priority-unassigned labels Mar 1, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants