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

Arguments placeholders for callbacks #837

Open
mixty1 opened this issue Dec 7, 2023 · 3 comments
Open

Arguments placeholders for callbacks #837

mixty1 opened this issue Dec 7, 2023 · 3 comments
Labels
proposal Proposal or discussion about a significant language feature

Comments

@mixty1
Copy link

mixty1 commented Dec 7, 2023

It would be nice, if you add to civet something like this

I have this code:

export default function throttle(func: (...args: any) => () => void, ms: number)

  isThrottled .= false
  savedArgs: any | null .= null
  savedThis: any | null .= null

  function wrapper(this: any): void
    if isThrottled
      savedArgs = arguments
      savedThis = this
      return

    func.apply this, arguments

    isThrottled = true

    setTimeout =>
      isThrottled = false
      if savedArgs
        wrapper.apply savedThis, savedArgs
        savedArgs = savedThis = null
    ,ms

  wrapper

And I would like to have such an opportunity as:

export default function throttle(func: (...args: any) => () => void, ms: number)

  isThrottled .= false
  savedArgs: any | null .= null
  savedThis: any | null .= null

  function wrapper(this: any): void
    if isThrottled
      savedArgs = arguments
      savedThis = this
      return

    func.apply this, arguments

    isThrottled = true

    setTimeout(&, ms) =>
      isThrottled = false
      if savedArgs
        wrapper.apply savedThis, savedArgs
        savedArgs = savedThis = null

  wrapper
someFunc(&, arg1, arg2, &, arg3)
  (argsFromFirstPlaceholder) =>
    // ...some code here
  (argsFromSecondPlaceholder) =>
    // ...some code
@mixty1 mixty1 changed the title [Feature request] An argument placeholders for callbacks [Feature request] Arguments placeholders for callbacks Dec 7, 2023
@edemaine
Copy link
Collaborator

edemaine commented Dec 7, 2023

I hadn't realized it before, but the one-argument case is a special case of #480, which increases the motivation for that proposal. It looks like imba also only supports one argument?

The two-argument version is interesting. Indentation application already does this:

someFunc
  (argsFromFirstPlaceholder) =>
    // ...some code here
  (argsFromSecondPlaceholder) =>
    // ...some code
↓↓↓
someFunc(
  (argsFromFirstPlaceholder) => {
    // ...some code here
  },
  (argsFromSecondPlaceholder) => {
    // ...some code
  }
)

So what this suggests is that someFunc(&, arg1, arg2, &, arg3) should map to ($1, $2) => someFunc($1, arg1, arg2, $2, arg3). Seems a reasonable generalization, though it's maybe a little counterintuitive that the &s don't refer to the same parameter; see #85 for possible alternatives.

@edemaine edemaine added the proposal Proposal or discussion about a significant language feature label Dec 7, 2023
@mixty1 mixty1 changed the title [Feature request] Arguments placeholders for callbacks Arguments placeholders for callbacks Dec 7, 2023
@mixty1
Copy link
Author

mixty1 commented Dec 7, 2023

It looks like imba also only supports one argument?

Probably yes.
By the way, if i'm not mistaken swift lang has similar syntax, but without placeholders, just callback after function call or something like that.

So what this suggests is that someFunc(&, arg1, arg2, &, arg3) should map to ($1, $2) => someFunc($1, arg1, arg2, $2, arg3)

With defined default parameter with callback functions:

($1 = (argsFromFirstPlaceholder) => { /* ...some code here */ }, $2 = (argsFromSecondPlaceholder) => { /* ...some code */ }) => someFunc($1, arg1, arg2, $2, arg3)

from this code:

someFunc(&, arg1, arg2, &, arg3)
  (argsFromFirstPlaceholder) =>
    // ...some code here
  (argsFromSecondPlaceholder) =>
    // ...some code

Seems a reasonable generalization, though it's maybe a little counterintuitive that the &s don't refer to the same parameter;

Maybe for that case, if has multiple different placeholders, then numerate it like:

someFunc(&1, arg1, arg2, &2, arg3)
  (argsFromFirstPlaceholder) =>
    // ...some code here
  (argsFromSecondPlaceholder) =>
    // ...some code

or add labels:

someFunc(&cb1, arg1, arg2, &cb2, arg3)
  (argsFromFirstPlaceholder) =>
    // ...some code here
  (argsFromSecondPlaceholder) =>
    // ...some code

@edemaine
Copy link
Collaborator

edemaine commented Oct 10, 2024

Looking back at this, the idea seems close to what's offered by . placeholders (#1070). In particular, we have:

(setTimeout ., 1000)
  => console.log 'hello world'
---
(($) => setTimeout($, 1000))(() =>
  console.log("hello world"),
);

Unfortunately, currently:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal Proposal or discussion about a significant language feature
Projects
None yet
Development

No branches or pull requests

2 participants