-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Language: Add null-conditional operators and null-propagation in Dart #21695
Comments
Added Area-Language, Triaged labels. |
This comment was originally written by @zoechi Seems similar to http://dartbug.com/41 |
I'm a little worried about the "null propagation" described here. In Dart, null is an object, and it has methods, so this null propagation would prevent calling those methods. Example: It has non-local effect - changing one call affects other calls. That makes some otherwise safe rewrites no longer preserve semantics: I'd rather not have a case where |
This comment was originally written by @Emasoft lrn: you are missing the point. Having a situation where foo.convert().toString() evaluate differently than (foo.convert()).toString() is just what we want to avoid, because it creates confusion and impredictability. |
The difference between C# and Dart is that in C#, a null is not an object, but in Dart, it is. In C#, if you do foo.bar().baz(), and foo is null, then bar always fails. In Dart you don't know that the .baz() call throws. Someone writing foo?.bar().toString() might want to always return a string. He knows that toString exists on all objects, including null. I'm also not satisfied with using expressions as delimiters for null propagation. I'd rather have some sort of explicit delimiter, like: I know it gives flashbacks to Java's strictfp, and deliberately so. It's just a horrible idea to have two different meanings of the same syntax depending on a non-local marker. Let's not do that. |
This comment was originally written by @Emasoft @gbracha: Obviously? Sorry but a "clean and local" implementation of the null conditional dot operator ?. is redundant, because without null propagation it requires more complex code and more calls that impact performances. Let me explain this with an example. if( data != null If you have just the null conditional operator, you can rewrite the code: var location = data.Address[0] as this: var location = data?.Address[0] With the null propagation you would have the option to explicitly check only a single call, for example: var location = data.Address[0] If that call is null because the list is empty, all subsequent calls on that objects are ignored. Without null propagation you have to explicitly add the null conditional operator to all method calls, slowing down the execution time. |
Can I achieve null-propagation with functions (not methods)? Let's say I have a value |
The ability to "call off" a function invocation based on some arguments being null has been raised before. One thing to consider here is readability: dart-lang/language#219 (comment). Kotlin |
This issue was originally filed by @Emasoft
It would be very useful to have Null-Conditional Operators and Null-Propagation in Dart.
The null-conditional has two syntax forms.
First, is available as a member operator, adding the question mark prior to the dot operator ( ?. ). When this is present if the value of the object is null, the null-conditional operator will return null. For example:
return value?.substring(0, length);
Second, is to use the question mark in combination with the index operator ( ?[…] ), causing indexing into collection only to occur if collection isn’t null. For example:
T? item = collection?[index];
Null-Propagation is a way to avoid null exceptions avoiding all additional invocations in the call chain if the operand is null. For example:
return value?.substring(0, length).padRight(3);
If .substring() is called via the null-conditional operator, and the null value?.substring() could seemingly return null, Null-Propagation will short-circuits the call to padRight(), and immediately returns null, avoiding the programming error that would otherwise result in a null reference exception.
See for example the C# implementation:
http://msdn.microsoft.com/en-us/magazine/dn802602.aspx
The text was updated successfully, but these errors were encountered: