Civet
Civet copied to clipboard
Arguments placeholders for callbacks
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
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.
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
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:
- The parentheses are required.
-
Without any parens, the arrow function is an argument to
1000
. This unfortunately seems "right" according to the low precedence of implicit function application. -
With parens around just the arguments, the
.
lifts outside the second call. I think this is a bug (#1449).
-
Without any parens, the arrow function is an argument to
-
.
doesn't work with multiple arguments. For example,setTimeout ., .
means$ => setTimeout($, $)
, not($1, $2) => setTimeout($1, $2)
. Potentially this is just the WIP state, though, and we could generalize (though it's not mentioned in #1070 — it only mentions&1, &2
, but.1
and.2
are numbers).