Proposal: Placeholder operator

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Proposal: Placeholder operator

Sultan
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)



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Placeholder operator

Claude Pache


> Le 11 janv. 2019 à 14:02, Sultan <[hidden email]> a écrit :
>
> Placeholder operator: !
>
> Use in function parameters(maintains arity without creating a named binding):
>
> const foo = (a, b, !) => {}

Today, you can write:

```js
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:

```js
const [ , setState] = useState(0)
```

—Claude
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Placeholder operator

Sultan
>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 5:31 PM Claude Pache <[hidden email]> wrote:


> Le 11 janv. 2019 à 14:02, Sultan <[hidden email]> a écrit :
>
> Placeholder operator: !
>
> Use in function parameters(maintains arity without creating a named binding):
>
> const foo = (a, b, !) => {}

Today, you can write:

```js
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:

```js
const [ , setState] = useState(0)
```

—Claude

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Placeholder operator

Tab Atkins Jr.
On Fri, Jan 11, 2019 at 6:42 AM Sultan <[hidden email]> 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!)

~TJ
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss