-
Notifications
You must be signed in to change notification settings - Fork 455
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
use arrow syntax for anonymous functions #6945
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the motivation for this? Shorter code?
Yes. It improves the readability of the output, especially where pipe syntax is used. There is no difference in the runtime performance between anonymous functions that already allow implicit captures. However, it might be a breaking change because it implicitly changes the behavior of |
@cometkim would you get feedback in discord? |
I like this! Just a few thoughts.
Could you add one or two examples of this to the description?
While not idiomatic, this type of thing is sometimes necessary/useful. Would it be possible to somehow opt into a regular function like we're emitting now when necessary? |
If |
I like this, too! 😄 In the case of a single argument, could/should we emit |
If we want. I already implemented it in my local branch |
@DZakh Can you confirm that this doesn't harm your library perf? |
I rely on |
Also, this change should positively impact the bundlesize of the projects. When I did benchmarks for rescript-schema, I noticed, that esbuild doesn't optimise |
How would this opt-in look like syntactically? Something like let x = [0, 1]->Array.map(@NoArrow x => x + 1) ? Maybe this could be an interim solution until all libraries / non-idiomatic code relying on this has been migrated to support arrow function output? |
Yep, I thought the similar (mine was I also checked if it could be a named function, but it must be anonymous because that would break the behavior. |
Maybe something like this, but for regular functions as well? https://rescript-lang.org/syntax-lookup#this-decorator |
This PR wouldn't change regular function behavior. I think we can support |
Missed the fact that the change is only for anonymous functions. Then there's no problem from my side. I don't see a good usecase for |
Just need to double check that there are tests for |
@DZakh @zth Methods using Tests are exist here: https://github.com/cometkim/rescript-compiler/blob/emit-arrow/jscomp/test/test_bs_this.js |
I would want to 😄. What do you say @zth @cristianoc? |
@cknitt sure! |
@cknitt check it out! |
This comment was marked as resolved.
This comment was marked as resolved.
Fixed it, but it's a heuristic. It would be a better implementation if it could recognize IIFE pattern from the Lambda IR. |
Just to double check: are there any concerns left about correctness, or have they all been answered? |
If everyone has checked the output, I think it's OK. Concerned parts like IIFE and The rest is just to make the output a little more "pretty", but it doesn't have to do with correctness. E.g.function findAllAddresses(businesses) {
return Belt_List.toArray(Belt_List.flatten(Belt_List.fromArray(Belt_Array.map(businesses, business => Belt_List.concat(Belt_Option.mapWithDefault(business.address, /* [] */0, a => ({
hd: a,
tl: /* [] */0
})), Belt_Option.mapWithDefault(business.owner, /* [] */0, p => Belt_Option.mapWithDefault(p.address, /* [] */0, a => ({
hd: a,
tl: /* [] */0
}))))))));
} function findAllAddresses(businesses) {
return Belt_List.toArray(
Belt_List.flatten(
Belt_List.fromArray(
Belt_Array.map(
businesses,
business => Belt_List.concat(
Belt_Option.mapWithDefault(
business.address,
/* [] */0,
a => ({
hd: a,
tl: /* [] */0
})
),
Belt_Option.mapWithDefault(
business.owner,
/* [] */0,
p => Belt_Option.mapWithDefault(
p.address,
/* [] */0,
a => ({
hd: a,
tl: /* [] */0
})
)
)
)
)
)
)
);
} |
About not wrapping args with |
Yes, but
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did not notice any further issues in the compiler output.
Great work, thanks a lot! 👍
Since it's JavaScript, I think it should be formatted as idiomatic JavaScript. And even though it's an extra noise, the majority of Js devs already got used to it and find it more natural to see paranthes than not. |
Maybe I am biased because I always have
in my prettier config. |
Anyway, how do we come to a decision regarding the arrow parens? |
So is mine. Most of the projects I'm involved with use the style of avoiding it. It's more of a personal preference. But I can agree with the |
I'll leave it as is, unless there are other objections. The complexity I mentioned is trivial. Changing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No special opinions as long as it does not break anything, and people are generally happy.
No description provided.