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

Empty object destructuring could tolerate null and undefined #1205

Open
pygy opened this Issue May 24, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@pygy

pygy commented May 24, 2018

In strict mode, this function declaration is a SyntaxError:

function third (_, _, x) {return x}

A nice alternative is

function third ({}, {}, x) {return x}

But then third(null, undefined, 5) throws a TypeError. It would be great if it didn't.

The change is AFAICT Web compatible since this is currently an error. It also seems logical to tolerate nulls here since the empty destructuring doesn't try to get anything from its source value.

This may be at odds with #1163 (or at least it would be useful to think both issues holistically).

Edit: cc @barneycarroll whose tweet triggered this message

@claudepache

This comment has been minimized.

Show comment
Hide comment
@claudepache

claudepache May 24, 2018

Contributor

For me, {/* whatever */} as function parameter suggests an object to be destructured, regardless whether /* whatever */ is empty or not (and the emptiness of /* whatever */ might change in the course of refactoring); so that the TypeError in third(null, undefined, 5) is justified.

Instead of some clever hack, why not just use something boring like:

function third(_1, _2, x) { return x }
function third(dummy, dummy2, x) { return x }

?

Contributor

claudepache commented May 24, 2018

For me, {/* whatever */} as function parameter suggests an object to be destructured, regardless whether /* whatever */ is empty or not (and the emptiness of /* whatever */ might change in the course of refactoring); so that the TypeError in third(null, undefined, 5) is justified.

Instead of some clever hack, why not just use something boring like:

function third(_1, _2, x) { return x }
function third(dummy, dummy2, x) { return x }

?

@pygy

This comment has been minimized.

Show comment
Hide comment
@pygy

pygy May 24, 2018

Destructuring in function signatures is about binding, and function ({}, x) { return x } is in general a great, native way to say "Please don't bind any name to the first argument", not a clever hack.

FWIW, it already tolerates non-objects like strings, numbers and booleans.

Edit: we could alternatively devise a new third ( , , x) {return x} syntax, but I'm not a fan of those lone comas even though there is a precedent with [ , , ]. It would also make weird TypeScript/Flow type signature if you wanted your function to ignore the params, yet be picky type-wise about what it ignores.

pygy commented May 24, 2018

Destructuring in function signatures is about binding, and function ({}, x) { return x } is in general a great, native way to say "Please don't bind any name to the first argument", not a clever hack.

FWIW, it already tolerates non-objects like strings, numbers and booleans.

Edit: we could alternatively devise a new third ( , , x) {return x} syntax, but I'm not a fan of those lone comas even though there is a precedent with [ , , ]. It would also make weird TypeScript/Flow type signature if you wanted your function to ignore the params, yet be picky type-wise about what it ignores.

@barneycarroll

This comment has been minimized.

Show comment
Hide comment
@barneycarroll

barneycarroll May 24, 2018

I support this proposal. I think explicitly destructuring to an empty object literal is clear enough in its intent to produce no reference: if the destructuring expression contained a property reference, then it's clear there would be a problem with interpolating undefined. As it is, the stated reason for the failure to destructure undefined values to {} don't match the stated rationale: it's perfectly possible to do this for non-nullish values, and since there's no attempted property interpolation it's not clear why nullish values throw.

barneycarroll commented May 24, 2018

I support this proposal. I think explicitly destructuring to an empty object literal is clear enough in its intent to produce no reference: if the destructuring expression contained a property reference, then it's clear there would be a problem with interpolating undefined. As it is, the stated reason for the failure to destructure undefined values to {} don't match the stated rationale: it's perfectly possible to do this for non-nullish values, and since there's no attempted property interpolation it's not clear why nullish values throw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment