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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safe navigation operator (?.) #549

Closed
HamedFathi opened this issue Jul 26, 2019 · 14 comments
Closed

Safe navigation operator (?.) #549

HamedFathi opened this issue Jul 26, 2019 · 14 comments
Assignees
Labels
API Committed Docs Documentation needs to be added/improved/corrected Suggestion Topic: Binding Issues related to binding/observation Topic: Compiling/rendering
Projects
Milestone

Comments

@HamedFathi
Copy link
Collaborator

HamedFathi commented Jul 26, 2019

馃檵 Feature Request

Recently, Optional Chaining for JavaScript went to stage 3. Seems we will achieve this feature in JS and TS soon.

Milestone: Typescript 3.7
Suggestion: "safe navigation operator", i.e. x?.y

Angular 2+ supports this feature long time ago as The safe navigation operator ( ? ) and null property paths

<p>The item name is: {{item?.name}}</p>

Is it possible to support this operator in Aurelia vNext template engine too?

馃 Expected Behavior

Skip rendering if the item is null or undefined.

馃槸 Current Behavior

We don't have this operator now.

馃捇 Examples

<p>The item name is: ${item?.name}</p>
@3cp
Copy link
Member

3cp commented Jul 26, 2019

Even aurelia v1 has this feature implicitly built-in in expression. We don鈥檛 need the syntax, because our expression always does the check to avoid runtime exception.

@fkleuver
Copy link
Member

We don鈥檛 need the syntax, because our expression always does the check to avoid runtime exception.

This syntax is coming to the ES spec and our intent with the expression syntax is to stay as close to native as possible. With this syntax, there is a clean and easy way to get this kind of safety whilst also being able to opt out of it (by not using the syntax).

So while we don't need the syntax, I still think we should have it, because it improves consistency.

@3cp
Copy link
Member

3cp commented Jul 26, 2019

It will be a not obvious breaking change for Aurelia 2 from v1. So some decision has to be made.

@davismj
Copy link
Member

davismj commented Jul 27, 2019

Doesn't sound breaking to me. If I understand Fred, the only difference is that the Expression parser will now understand {prop1?.prop2}, which would have failed to parse in au1.

@HamedFathi
Copy link
Collaborator Author

@fkleuver

I think you should consider ?? too.

Current Stage: Stage 3
Nullish Coalescing for JavaScript

Milestone: Typescript 3.7
Nullish coalescing operator (??)

@fkleuver
Copy link
Member

@davismj No, my idea is to switch the behavior to null-coalescing operator and then to have the vCurrent compat package turn on a flag where the old behavior is retained, but with the new behavior it would be a breaking change. And it is a good one imo, because:

  1. It could easily be toggled for those who don't want to spend the migration effort (or those who simply don't like the syntax)
  2. It gives people a way to let the framework actually throw if they want to be aware of when stuff is null when it shouldn't be
  3. It makes the new syntax more consistent with real javascript

@HamedFathi Yep, agree, will get that in too

@rmja
Copy link

rmja commented Aug 14, 2019

Please, don't introduce the ?. syntax in views. It will introduce unnecessary null reference errors without any possible gain. Other view "languages" like vCurrent, handlebars, and Razor all swallows null references making the ?. implicit.

@davismj
Copy link
Member

davismj commented Aug 14, 2019

@rmja while I personally appreciate the flexible syntax, I've squashed LOTS of bugs that have to do with properties being uninitialized. While some would prefer the flexibility and say the responsibility falls on the developer, others prefer a more explicit syntax, which would warn them when they've introduced a potential bug into the system.

As long as the more flexible syntax is easily toggled, I think the introduction will be a welcome change.

@fkleuver
Copy link
Member

@rmja As I explained in the previous comment, it would be a toggle, a strictness option. I would probably use ?. in my views (and I'm sure so would many others) so as to be aware of when an object is null when it shouldn't be.

If we forced this on everyone, this in itself would be a massive breaking change for no good reason. Of course we'll avoid anything like that. So, no one would be forced to use the syntax so at the very least migration from v1 stays as painless as possible

@CuddleBunny
Copy link

Rather than a feature flag or compatibility package for the operator itself why not flag the strictness of template evaluations and the behavior of null in general? Perhaps something along the lines of "template strict evaluation" and "template render null as default"?

Default: ${test.name} and ${test?.name} should both behave the same with the latter being the preferred convention because it better conveys the actual behavior. In vCurrent the latter throws a parser error.

Strict: the first would throw ReferenceError: test is not defined and the latter would defer to another setting that specifies whether or not to allow null through or also throw. Turning on strict and disallowing null template evals would then force us to write ${test?.name || ''} or even ${test?.name ?? ''}.

@fkleuver
Copy link
Member

fkleuver commented Sep 4, 2019

Agree @CuddleBunny
This is pretty much what I meant to say, but you phrased it more clearly.

@fkleuver fkleuver added this to Triage in Work queue Oct 7, 2019
@fkleuver fkleuver moved this from Triage to Backlog in Work queue Oct 7, 2019
@fkleuver fkleuver self-assigned this Oct 7, 2019
@fkleuver fkleuver added API Committed Docs Documentation needs to be added/improved/corrected Suggestion Topic: Binding Issues related to binding/observation Topic: Compiling/rendering labels Oct 7, 2019
@fkleuver fkleuver added this to the Backlog milestone Oct 7, 2019
@ghiscoding
Copy link
Contributor

Side note, TypeScript 3.7.2 with Optional Chaining and Nullish Coalescing is now live and is the final release version. You can get it from NPM and read the Microsoft Blog Post.

Finally we'll get some cleaner code, hopefully Aurelia will start using it..
Cheers

@davismj
Copy link
Member

davismj commented Nov 6, 2019

Aurelia taught TypeScript everything it knows about optional chaining.

@Vheissu
Copy link
Member

Vheissu commented Sep 6, 2022

Closing this as supported for optional syntax and bullish coalescing has been merged in thanks to the work of @fkleuver

@Vheissu Vheissu closed this as completed Sep 6, 2022
Work queue automation moved this from Backlog to Done Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Committed Docs Documentation needs to be added/improved/corrected Suggestion Topic: Binding Issues related to binding/observation Topic: Compiling/rendering
Projects
Work queue
  
Done
Development

No branches or pull requests

8 participants