You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dart has nice null safety support with it's awesome operators, null check ! and null aware ?.
The actual null aware is nice, but I was thinking about having a short syntax to deal with nullable variables in some better way. See the following example and how we can reduce the cost of typing multiple if statements. I have found this code in the flutter repo, there is a lot like this.
Actually:
@overrideIterable<RenderBox> get children {
return<RenderBox>[
if (leading !=null)
leading!,
if (title !=null)
title!,
if (subtitle !=null)
subtitle!,
if (trailing !=null)
trailing!,
];
}
Proportional
Introduce a ?! operator, this new operator converts nullable variables to non-nullable only if they're not null. Nullable variables which is not assigned will be ignored. I'm proposing to introduce a new ignorable variables concept, ignorable variable will be skipped by the compiler if it holds a null value. I'm not sure if we can introduce this new operator without intruding the ignorable type.
The same example of before, but with the new ?! operator:
@overrideIterable<RenderBox> get children {
return<RenderBox>[
leading?!,
title?!,
subtitle?!,
trailing?!,
];
}
Out of list context:
String name ="Olivier";
String? _lastName;
// Concatenate _lastName if it's not null, otherwise return only name.// It's sounds like having a default value, but it's not. We simply ignored the _lastName variable. Stringget fullName => name + _lastName?!;
// Make the compiler ignoring the lastName getter and it's usages. Stringget lastName => _lastName?!;
Ignorable deceleration:
String?! name; // hold null or special value to indicate that this variable is ignorable, may `ignored` or `undefined`// never will be called. print(name);
flutter context random examples:
// ignorable String?! name;
Text(name);
// actuallyString? name;
name !=null?Text(name!) :null;
// null to ignorableText(name?!);
Conclusion
If we're able to deal with nullables as ignorables by only using the ?! operator it'll awesome since we'll have no breaking changes. I hope I was clear, Thanks for your time, special thanks to Dart team.
Good coding!
The text was updated successfully, but these errors were encountered:
The only place where I can see this working is in a collection literal, where it makes sense to omit a value. It doesn't work in general expressions, because they must have a value.
We cannot simply ignore an expression. If you do name + _lastName?!, and ignore the _lastName expression, the + is still there and needs an argument.
The Text(name) example also only works if the parameter of Text is optional, otherwise there is no value to pass to the constructor.
If it's just collection elements, then #323 should solve the same problem.
The problem
Dart has nice null safety support with it's awesome operators, null check
!
and null aware?
.The actual null aware is nice, but I was thinking about having a short syntax to deal with nullable variables in some better way. See the following example and how we can reduce the cost of typing multiple if statements. I have found this code in the flutter repo, there is a lot like this.
Actually:
Proportional
Introduce a
?!
operator, this new operator converts nullable variables to non-nullable only if they're not null. Nullable variables which is not assigned will be ignored. I'm proposing to introduce a newignorable variables
concept, ignorable variable will be skipped by the compiler if it holds a null value. I'm not sure if we can introduce this new operator without intruding the ignorable type.The same example of before, but with the new
?!
operator:Out of list context:
Ignorable deceleration:
flutter context random examples:
Conclusion
If we're able to deal with nullables as ignorables by only using the
?!
operator it'll awesome since we'll have no breaking changes. I hope I was clear, Thanks for your time, special thanks to Dart team.Good coding!
The text was updated successfully, but these errors were encountered: