Skip to content

Omit trailing undefined #6652

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

Closed
wants to merge 5 commits into from
Closed

Omit trailing undefined #6652

wants to merge 5 commits into from

Conversation

zth
Copy link
Collaborator

@zth zth commented Feb 26, 2024

Fixes #6543

This omits trailing undefineds when applying external functions with optional arguments, when the optional argument itself is not provided (or is None). Only applies to external calls, because that's the only place where it could matter.

Some more background: ReScript v11 with uncurried mode allows us to write much more ergonomic external function bindings because we can have optional arguments where we don't need a trailing unit to apply the function itself. But, this comes with a potential problem. All arguments, whether they're actually supplied or not, would be printed as undefined in the resulting JS. External JS calls might care about the number of args sent into it, so someJsFn(1) and someJsFn(1, undefined) is not always treated the same. Several JS APIs can break if provided the wrong amount of arguments unless intended.

cc @cristianoc what do you think about this?

@zth zth requested review from cknitt and cristianoc February 26, 2024 17:30
Comment on lines +6 to +12
SomeModule.formatDate(new Date());

SomeModule.formatDate(new Date(), {
someOption: true
});

var x = SomeModule.formatDate(new Date(), undefined, true);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the difference of these 3. The first one has 2 optional args omitted, and because of that get no undefineds. The second one has the last optional arg omitted, and because of that skips the trailing undefined too.
The third call has the last optional arg set, and therefore gets undefined in between so the args end up in the right positions.

Copy link
Member

@cknitt cknitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff! 🎉

@cristianoc
Copy link
Collaborator

This is great!
Looking at the code now.

Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great that it could be done with a simple local transformation.

@zth zth changed the base branch from master to 11.0_release February 27, 2024 08:04
@zth zth changed the base branch from 11.0_release to master February 27, 2024 08:08
@zth
Copy link
Collaborator Author

zth commented Feb 27, 2024

Superseded by #6653 (I messed up git)

@zth zth closed this Feb 27, 2024
@zth zth deleted the omit-trailing-undefined branch April 29, 2025 09:06
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

Successfully merging this pull request may close these issues.

Omit trailing undefineds from function invocations in JS output
3 participants