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

& function shorthand revamp, and ./... argument placeholders #1070

Open
2 of 4 tasks
edemaine opened this issue Feb 27, 2024 · 2 comments
Open
2 of 4 tasks

& function shorthand revamp, and ./... argument placeholders #1070

edemaine opened this issue Feb 27, 2024 · 2 comments
Labels
partially completed Some of the requested functionality has already been implemented proposal Proposal or discussion about a significant language feature

Comments

@edemaine
Copy link
Collaborator

edemaine commented Feb 27, 2024

Based on Discord discussion, kicked off with a big idea from @STRd6:

  • & precedence
  • . placeholder
  • ... placeholder
  • &0, &1, etc.

& precedence [DONE]

I think the rule would be lift the & function to be either at statement level or within a function application, whichever is encountered first

Alternative wording:

The scope for the function wrapping & is up to but not including function calls, stopping at beginning of expression [so not including e.g. the left part of a declaration]

Thus:

[&, x]          $ => [$, x]
[&, &]          $ => [$, $]
& + &           $ => $ + $
[...&]          $ => [...$]
[[&]]           $ => [[$]]
{text: &}       $ => {text: $}
{&: "value"}    $ => {[$]: "value"}
x > &           $ => x > &
(& + 1) % n     $ => ($ + 1) % n
f ...& // invalid, at least for now

Note that the rule reasonably solves #85, allowing for things like &+& and [&, &]. If you want two different functions, you need to separate them into different statements or calls. In the array example, we could write [(do &), (do &)]. Or perhaps [&x, &y].

Some of these examples are from #480, which I would view this as replacing. But some of those examples don't work:

f(a, &, b)
console.log &
x.filter !Array.isArray &

These don't have a clear meaning with the new rule.

  • Perhaps & that can't be lifted is just ($) => $. We can't escape a function call this way. But we'll propose another way next!
  • Or perhaps & does get lifted one level when it's lonely like this, as in & function shorthand improvements #480. Feels a bit asymmetric. And wait for the next part!

. argument placeholder [DONE]

At the top level of function arguments and array literals, we propose that . adds a magic function wrapper one level up:

f(a, ., b)      $ => f(a, $, b)
console.log .   $ => console.log($)
x.filter !Array.isArray .   // leading unary operators get pulled in???
                x.filter($ => !Array.isArray($))
[a, ., b]       $ => [a, $, b]
[., .]          $ => [$, $]

This gives us a form of partials and placeholders (#75):

last |> f a, b, .      f(a, b, last)
array |> [x].concat .     [x].concat(array)

... arguments placeholder

In contexts where ...x makes sense (function arguments, array literals, and object literals), ... also adds a magic function wrapper one level up, but with multiple arguments:

f(a, ..., b)      (...$) => f(a, ...$, b)
console.log ...   (...$) => console.log(...$)
x.filter !Array.isArray (...)   // leading unary operators get pulled in???
                x.filter((...$) => !Array.isArray(...$))
[a, ..., b]       (...$) => [a, ...$, b]
[..., ...]        (...$) => [...$, ...$]

This gives us a function composition operator: to compose functions like f >> g in F#:

f ... |> g      (...$) => g(f(...))

If you don't worry about the extra wrappers, it's also a solution to "currying" (#818):

f a, ...       (...$) => f(a, ...$)

Multiple Arguments with &

Back to &, we can now easily allow for multiple arguments too. & acts like &0, &1 accesses the next argument, etc.

array.filter [&1, &0]      array.filter(($0, $1) => [$1, $0])
@edemaine edemaine added the proposal Proposal or discussion about a significant language feature label Feb 27, 2024
@bbrk24
Copy link
Contributor

bbrk24 commented Apr 11, 2024

... in object literals doesn't seem terribly useful:
image

@bbrk24
Copy link
Contributor

bbrk24 commented Apr 18, 2024

. does not currently work in constructors (e.g. x |> new Foo ., y).

edemaine added a commit that referenced this issue Apr 20, 2024
edemaine added a commit that referenced this issue Apr 20, 2024
New ampersand proposal (#1070)

BREAKING CHANGE: & can lift the function wrapper to higher than before
@edemaine edemaine added the partially completed Some of the requested functionality has already been implemented label Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
partially completed Some of the requested functionality has already been implemented proposal Proposal or discussion about a significant language feature
Projects
None yet
Development

No branches or pull requests

2 participants