make dealing with null less painful #21222
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
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?
The text was updated successfully, but these errors were encountered: