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

Generate optional chains #168

Open
iccir opened this issue Mar 4, 2023 · 0 comments
Open

Generate optional chains #168

iccir opened this issue Mar 4, 2023 · 0 comments
Milestone

Comments

@iccir
Copy link
Member

iccir commented Mar 4, 2023

Currently, we need to use temporary variables to store the result of each message. For example:

[[[anObject foo] bar] baz]

becomes:

var N$_t_0,N$_t_1;
((N$_t_0 = (((N$_t_1 = ((anObject && anObject.N$_f_foo())))
    && N$_t_1.N$_f_bar()))) && N$_t_0.N$_f_baz());

Using optional chaining, we can eliminate these temporary variables:

anObject?.N$_f_foo()?.N$_f_bar()?.N$_f_baz()

Unfortunately, optional chaining returns undefined rather than null when it short-circuits. During implicit conversions, undefined becomes NaN rather than 0. Hence, the following:

[anObject foo] + 5

will now be NaN if anObject is null.

We can use nullish coalescing to ensure that messaging results are null:

(anObject?.N$_f_foo()?.N$_f_bar()?.N$_f_baz())??null


Note: this would subtly change falsy messaging - messaging undefined would return null instead of undefined.

iccir added a commit that referenced this issue Mar 14, 2023
@iccir iccir added this to the 3.0 milestone Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant