Proposal: Placeholder operator
Any reason you don't like the empty space with a comma? I also don't, but that's what it is now:
arr.map(( , index) => ...)
Also wouldn't mind ? or , since I associate ! with "not" as a unary operator, but "" isn't a unary in JS.
Le 11 janv. 2019 à 14:02, Sultan <thysultan at gmail.com> a écrit :
Placeholder operator: !
Use in function parameters(maintains arity without creating a named binding):
const foo = (a, b, !) => {}
Today, you can write:
const foo = (a, b, _) => { }
Is the complexity added to the language worth the feature?
Use in de-structuring(allows you to capture further along a tuple without creating a named binding):
const [!, setState] = useState(0)
You can already write:
const [ , setState] = useState(0)
empty space with a comma?
I think that only works with trailing params. For example this is not possible:
const foo = (a, , c) => {}
Today, you can write: const foo = (a, b, _) => {}
However that does throw with:
const foo = (a, _, _) => {}
You can already write: const [ , setState] = useState(0)
Thanks i forgot about that.
On Fri, Jan 11, 2019 at 6:42 AM Sultan <thysultan at gmail.com> wrote:
empty space with a comma?
I think that only works with trailing params. For example this is not possible:
const foo = (a, , c) => {}
It doesn't even "work" with trailing params. Function arglists can
contain a trailing comma, it's just ignored and does nothing. const foo = (a, b, )=>a+b
still has a .length == 2, and (a, b, ,)=>a+b
is
just invalid syntax. The trailing comma is just allowed so you can format a long arglist onto separate lines, and don't have to remember that the last argument doesn't have a comma.
Today, you can write: const foo = (a, b, _) => {}
However that does throw with:
const foo = (a, _, _) => {}
Yup, I've run into that problem before, and had to hack around by
using _1
for the second ignored arg. It's rare/niche enough that I'm
not sure it's worth solving, but I do acknowledge it as an annoyance.
You can already write: const [ , setState] = useState(0)
Thanks i forgot about that.
Yeah, using holes works, but it's definitely harder to read than using _.
And the fact that the last trailing , does not create a hole isn't
relevant for destructuring, but it would be relevant and confusing for
function-arity if we allowed holes in arglists. That is,
[1,2,].length
returns 2 list, while [1,2,,].length
returns 3. If
we get pattern-matching and array literals can do strict length
checking, it would also become relevant and confusing. (But that's
several stacked "if"s!)
Placeholder operator: !
Use in function parameters(maintains arity without creating a named binding):
const foo = (a, b, !) => {}
Use in de-structuring(allows you to capture further along a tuple without creating a named binding):
const [!, setState] = useState(0)