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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Operator: ?.() conditional function call #3804

Open
utenma opened this issue May 15, 2024 · 1 comment
Open

New Operator: ?.() conditional function call #3804

utenma opened this issue May 15, 2024 · 1 comment
Labels
feature Proposed language feature that solves one or more problems

Comments

@utenma
Copy link

utenma commented May 15, 2024

Dart supports two conditional access mecanisms

Dart

foo?.bar // member
fooList?[1]  // subscript

There is a third conditional access mecanism for conditional functions calls, currently available on javascript from MDN

You can use optional chaining when attempting to call a method which may not exist. This can be helpful, for example, when using an API in which a method might be unavailable, either due to the age of the implementation or because of a feature which isn't available on the user's device.

Javascript

foo?.prop // member
fooList?.[expr] // subscript

// ↓↓↓↓
func?.(args) // function
@utenma utenma added the feature Proposed language feature that solves one or more problems label May 15, 2024
@lrhn
Copy link
Member

lrhn commented May 15, 2024

That was considered, actually as maybeFun?(args), when Dart introduced ?[...] for null aware indexing.
It wasn't added, one reason being that it parses badly, another that maybeFun?.call(arga) is an adequate workaround.

Allowing a null-aware function call as maybeFun?.(args) is an option that has been considered as well.
It's just that ?.call is still a perfectly adequate alternative, so it's never been urgent to shorten that.
(And maybe we'd want the syntax for something else.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

2 participants