Replies: 4 comments
-
|
For what it's worth: other languages I know that have null access handling do short circuit C#: record Outer(Inner inner);
record Inner(string message);
Outer value = null;
outer?.inner.message; // nullJS: null?.inner.message; // undefinedRust: struct Outer { inner: Option<Inner> }
struct Inner { message: Option<String> }
fn example(outer: Option<Outer>) -> Option<String> {
let message = outer?.inner.unwrap().message.unwrap();
Some(message)
}
example(None)Not to sway one way or the other, just in case familiarity is an important part of using this feature. Personally I like annotating each segment that can be null |
Beta Was this translation helpful? Give feedback.
-
|
About the current implementation, with If missing baz raise an error, no matter what bar is, why should we precise that bar is optional. In other words, with the current implementation, those 2 expressions is similar : |
Beta Was this translation helpful? Give feedback.
-
|
I don't have a strong feeling either way. Happy to change the behaviour to short-circuit if people feel that that would be better. |
Beta Was this translation helpful? Give feedback.
-
|
PR created: #8554 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to talk about the recent change for the new syntax to get optional values (#8379)
There is one line in examples that confuses me :
I think it should instead short circuit baz as bar is not required.
Let me explain :
In case I would do
$var.bar?.baz, what I expect is bar is optional, but baz is required if bar is present.In other words : "if, in $var, there is no bar, return null. Otherwise, in bar, if there is no baz, this time, i want to raise an error"
Here is an example :
With the actual syntax, we are required to write
$var.bar?.baz?to get a null if bar is not present. But no matter how, the current line always returns null, it doesn't raise any error.My syntax I described above give much more flexibility on getting optional or required value
What do you think ?
12 votes ·
Beta Was this translation helpful? Give feedback.
All reactions